Skip to content Skip to sidebar Skip to footer

Python 2.7 Urllib2 Raising Urllib2.httperror 301 When Hitting Redirect With Xml Content

I'm using urllib2 to request a particular S3 bucket at hxxp://s3.amazonaws.com/mybucket. Amazon sends back an HTTP code of 301 along with some XML data (the redirect being to hxxp

Solution 1:

You are better off using the requests library. requests handle redirection by default : http://docs.python-requests.org/en/latest/user/quickstart/#redirection-and-history

import requests

response = requests.get(site)
print(response.content)

I don't get the problem with urllib2, I tried to look into the documentation https://docs.python.org/2/library/urllib2.html but it doesn't look intuitive.

It seems that in Python3, they refactored it to make it less a burden to use, but I am still convinced that requests is the way to go.

Note The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

Post a Comment for "Python 2.7 Urllib2 Raising Urllib2.httperror 301 When Hitting Redirect With Xml Content"