Skip to content

Instantly share code, notes, and snippets.

@wingyplus
Created May 13, 2013 09:34
resolve http redirect
import httplib, urlparse
def resolve_http_redirect(url, depth=10):
"""
>>> resolve_http_redirect('http://localhost:8080/share')
'http://localhost:8080/share/page/'
"""
o = urlparse.urlparse(url)
connection = httplib.HTTPConnection(o.netloc)
connection.request('GET', o.path)
response = connection.getresponse()
location = response.getheader('location')
if response.status == httplib.FOUND and location != None:
return resolve_http_redirect(location)
elif response.status == httplib.OK:
return url
if __name__ == '__main__':
print(resolve_http_redirect('http://localhost:8080/share'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment