import urllib.parse as urlparse import http.client as httplib #---------------------------------------------------------------------- def resolve_http_redirect(url, depth=0): """http://www.zacwitte.com/resolving-http-redirects-in-python """ if depth > 10: raise Exception("Redirected "+depth+" times, giving up.") o = urlparse.urlparse(url,allow_fragments=True) conn = httplib.HTTPConnection(o.netloc) path = o.path if o.query: path +='?'+o.query conn.request("HEAD", path) res = conn.getresponse() headers = dict(res.getheaders()) if 'Location' in headers and headers['Location'] != url: return resolve_http_redirect(headers['Location'], depth+1) else: return url