Authentication from urllib2

F

Fuzzyman

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
 
M

Michael Foord

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top