Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Created February 8, 2017 00:48
Show Gist options
  • Save magnetikonline/9d3aa5d3df53b6a445eda77a16db20bc to your computer and use it in GitHub Desktop.
Save magnetikonline/9d3aa5d3df53b6a445eda77a16db20bc to your computer and use it in GitHub Desktop.
Stop Python urllib2 following HTTP redirects.

Stop Python urllib2 following HTTP redirects

By default calling a URL via urllib2 will follow any 30X responses returned, often not what you want.

We can correct this behavior by adding a custom "opener" class to the urllib2 module like so:

import urllib
import urllib2


# install alternative handler to stop urllib2 from following redirects
class NoRedirectHandler(urllib2.HTTPRedirectHandler):
	# alternative handler
	def http_error_300(self,req,fp,code,msg,header_list):
		data = urllib.addinfourl(fp,header_list,req.get_full_url())
		data.status = code
		data.code = code

		return data

	# setup aliases
	http_error_301 = http_error_300
	http_error_302 = http_error_300
	http_error_303 = http_error_300
	http_error_307 = http_error_300

urllib2.install_opener(
	urllib2.build_opener(NoRedirectHandler())
)

And we're done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment