urllib2 Opener and Proxy/Authentication issues

R

Ray Slakinski

Hello,

I have defined a function to set an opener for urllib2, this opener
defines any proxy and http authentication that is required.

If the proxy has authencation itself and requests an authenticated file
I get a HTTP status code of 401 (Unauthorized access of the file being
requested) I do see in the headers the Proxy-authorization and the
Authorization headers being sent for the request.

Just to clarify, if I do not go through a proxy that has authentication
then I can get the file just fine.

Here is my code for the opener:

def setOpener(realm='', host='', uName=None, pWord=''):
opener = urllib2.build_opener(urllib2.HTTPHandler)

if len(settings.globalProxySetting) > 0:
proxies = {'http' : settings.globalProxySetting, 'https' :
settings.globalProxySetting}
if not uName == None:
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, host, uName, pWord)
auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(urllib2.ProxyHandler(proxies),
auth_handler, urllib2.HTTPHandler)
else:
opener = urllib2.build_opener(urllib2.ProxyHandler(proxies),
urllib2.HTTPHandler)
else:
if not uName == None:
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, host, uName, pWord)
auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(auth_handler)
else:
opener = urllib2.build_opener(opener)

urllib2.install_opener(opener)


ps: settings.GlobalProxySetting is defined as a string, for example:

non-authenticated proxy: "http://192.168.1.1:3128"
authenticated proxy: "http://user:[email protected]:3128"

Thanks in advance,
Ray
 
J

John J. Lee

Ray Slakinski said:
ps: settings.GlobalProxySetting is defined as a string, for example:

non-authenticated proxy: "http://192.168.1.1:3128"
authenticated proxy: "http://user:[email protected]:3128"
[...]

IIRC urllib2 is slightly broken wrt proxy auth and the @ syntax. I
think I even explained here or on wwwsearch-general mailing list what
needed fixing -- IIRC again, it's very simple to fix but of course
needs testing, a unit test case adding, posting a patch on the SF
tracker. Go for it!


John
 

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,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top