Ec5a58db4acc016568e0293d283cbc2d

I'm looking for the fastest way to make an HTTP request. Specially, I'm wondering if the value of the url should be assigned to a variable first and then passed as an argument of "urlopen", or can the HTTP request be made this way :

#!/usr/bin/python
# -*- coding: utf-8 -*-

from urllib import urlencode
from urllib2 import urlopen

URL = 'http://refactormycode.com/'
TINYURL = 'http://tinyurl.com/apicreate.php?%s'
ISGD = 'http://is.gd/api.php?%s'

def tiny_url(url):
    """ Query a Tinyurl service, if not available query an alternative one
        If the alternative service is not available, return the url """ 
    try:
        url = urlopen(ISGD % urlencode({'longurl':url})).read()
    except:
        url = urlopen(TINYURL % urlencode({'url':url})).read()
    finally:
        return url

print tiny_url(URL)

Refactorings

No refactoring yet !

Your refactoring





Format Copy from initial code

or Cancel