#!/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 !
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 :