Authentication session with urllib2

Jon Clements joncle at googlemail.com
Wed Nov 11 07:55:46 EST 2009


On 11 Nov, 07:02, Ken Seehart <k... at seehart.com> wrote:
> I'm having some difficulty implementing a client that needs to maintain
> an authenticated https: session.
>
> I'd like to avoid the approach of receiving a 401 and resubmit with
> authentication, for two reasons:
>
> 1. I control the server, and it was easy for me to make a url that
> receives a POST with username, password and authenticates the session.  
> The server keeps a session correctly when I test with a browser.  This
> part works fine.
>
> 2. I don't want to send every request twice.  Seehttp://bugs.python.org/issue7159 There's no reason why I should have to
> do this since I have the ability to keep the server simple.
>
> What I would really like to do is send one request with the username and
> password to establish the session, and then make multiple authenticated
> requests where the session information remembers the authentication.
>
> Is there a way to make this happen in python 2.5.2?
>
> Keep in mind that this only needs to work with a particular server which
> I control.  It does not need to function as a general purpose browser.  
> The server is powered by Django.
>
> - Ken

How about http://docs.djangoproject.com/en/dev/topics/auth/ and using
a urllib2 opener with cookie support ala some examples on
http://personalpages.tds.net/~kent37/kk/00010.html ?

hth,
Jon.



More information about the Python-list mailing list