Search code examples
pythonpython-2.7urllib2basic-authentication

Python urllib2 accesses page without sending authentication details


I was reading urllib2 tutorial wherein it mentions that in order to access a page that requires authentication (e.g. valid username and password), the server first sends an HTTP header with error code 401 and (python) client then sends a request with authentication details.

Now, the problem in my case is that there exist two different versions of a webpage, one that can be accessed without supplying any authentication details and one that is quite different when authentication details are supplied (i.e. when the user is logged in the system). As an example think about url www.gmail.com, when you are not logged in you get a log-in page, but if your browser remembers you from your last login then the result is your email account homepage with your inbox displayed.

I follow all the details to set up an handler for authentication and install an opener. However everytime I request the page get back the version of the webpage that does not have the user logged-in.

How can I access the other version of webpage that has the user logged-in?


Solution

  • Requests makes this easy. As its creators say:

    Python’s standard urllib2 module provides most of the HTTP capabilities you need, but the API is thoroughly broken.