unshorten url python

Solutions on MaxInterview for unshorten url python by the best coders in the world

showing results for - "unshorten url python"
Valerio
09 Mar 2017
1# This is for Py2k.  For Py3k, use http.client and urllib.parse instead, and
2# use // instead of / for the division
3import httplib
4import urlparse
5
6def unshorten_url(url):
7    parsed = urlparse.urlparse(url)
8    h = httplib.HTTPConnection(parsed.netloc)
9    h.request('HEAD', parsed.path)
10    response = h.getresponse()
11    if response.status/100 == 3 and response.getheader('Location'):
12        return response.getheader('Location')
13    else:
14        return url
15