Authentication from urllib2
Michael Foord
fuzzyman at gmail.com
Sat Oct 23 06:31:38 EDT 2004
Aaargh.. that was annoying... much searching on the net to work this one out.
No thanks to you lot though ;-)
theurl = 'http://www.somerestrictedserver.com'
passman = urllib2.HTTPPasswordMgr()
passman.add_password(therealm, theurl, username, password)
*won't* work.
theurl starts with the protocol http:// which isn't what HTTPPasswordMgr needs.
theurl = 'www.somerestrictedserver.com'
works...
Regards,
Fuzzy
http://www.voidspace.org.uk/atlantibots/pythonutils.html
Fuzzyman <fuzzyman at gmail.com> wrote in message news:<sr3jn0d4nu6ic5eksjdr6d2i7rmi31ds3n at 4ax.com>...
> I'm trying to do Basic authentication using urllib2 and
> HTTPPasswordMgr Objects.
>
> According to my understanding I ought to perform the following simple
> sequence (except it doesn't work). Can someone cast their eyes over it
> and see what I'm doing wrong please.............
>
> import urllib2
>
> theurl = 'http://www.somerestrictedserver.com'
> username = 'somebody'
> password = 'XXXXX' # a great password
> therealm = 'theRealm'
>
> passman = urllib2.HTTPPasswordMgr()
> passman.add_password(therealm, theurl, username, password)
>
> authhandler = urllib2.HTTPBasicAuthHandler(passman)
> opener = urllib2.build_opener(authhandler)
>
> req = urllib2.Request(theurl)
> filehandle = opener.open(req)
> print filehandle.read()
>
> Ideally I would like to use HTTPPasswordMgrWithDefaultRealm() and
> specify None for the realm. I would also like to install the opener
> and just use urllib2.urlopen rather than using the opener directly.
>
> When I run this I still get a 401 HTTPError. I would expect the opener
> to recognise the realm and retry the request (handle the error using
> hte handler I have provided..).
>
> Regards,
>
>
> Fuzzy
> http://www.voidspace.org.uk/atlantibots/pythonutils.html
More information about the Python-list
mailing list