urllib2 and HTTPBasicAuthHandler

Max Erickson maxerickson at gmail.com
Tue Jan 16 13:03:53 EST 2007


"m.banaouas" <banaouas.medialog at wanadoo.fr> wrote:

> Hi all,
> I started to use urllib2 library and HTTPBasicAuthHandler class
> in order to authenticate with a http server (Zope in this case).
> I don't know why but it doesn't work, while authenticating with
> direct headers manipulation works fine!
> 
...
> port':'8080','path':'manage','realm':'Zope'} 
>    url = '%(prot)s://%(host)s:%(port)s/%(path)s' % data
...

Are you certain that the realm on the server matches 'Zope'?

HTTPBasicAuthHandler uses the realm to look for the password, if the 
realm the server provides is different, urllib2 will respond as if it 
does not have a user/password.

If you are only doing one request, it isn't that big a deal; if you 
are doing many requests or want urllib2 to handle the response for 
other reseans, you can use a default realm, see the bottom of this 
example:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/305288

(The advantage of using a default rather than making sure the realm is 
correct is that it can change and you won't have to do anything)



max




More information about the Python-list mailing list