Is there an easy way to request a URL in python and NOT follow redirects? Is there an easy way to request a URL in python and NOT follow redirects? python python

Is there an easy way to request a URL in python and NOT follow redirects?


Here is the Requests way:

import requestsr = requests.get('http://github.com', allow_redirects=False)print(r.status_code, r.headers['Location'])


Dive Into Python has a good chapter on handling redirects with urllib2. Another solution is httplib.

>>> import httplib>>> conn = httplib.HTTPConnection("www.bogosoft.com")>>> conn.request("GET", "")>>> r1 = conn.getresponse()>>> print r1.status, r1.reason301 Moved Permanently>>> print r1.getheader('Location')http://www.bogosoft.com/new/location


This is a urllib2 handler that will not follow redirects:

class NoRedirectHandler(urllib2.HTTPRedirectHandler):    def http_error_302(self, req, fp, code, msg, headers):        infourl = urllib.addinfourl(fp, headers, req.get_full_url())        infourl.status = code        infourl.code = code        return infourl    http_error_300 = http_error_302    http_error_301 = http_error_302    http_error_303 = http_error_302    http_error_307 = http_error_302opener = urllib2.build_opener(NoRedirectHandler())urllib2.install_opener(opener)