def unshorten_url(url):
    parsed = urlparse.urlparse(url)
h = httplib.HTTPConnection(parsed.netloc)
resource = parsed.path
if parsed.query != '':
    resource += '?' + parsed.query
h.request('HEAD', resource)
response = h.getresponse()
if response.status / 100 == 3 and response.getheader('Location'):
    return unshorten_url(response.getheader('Location'))
else:
    return url
How can I un-shorten a URL using python?