I'm trying to do Basic authentication using urllib2 and HTTPPasswordMgr
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............
(Python 2.2.2 as it happens).
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)
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 the
handler I have provided..).