urllib2 Opener and Proxy/Authentication issues

Ray Slakinski ray.slakinski at gmail.com
Tue Nov 8 10:18:00 EST 2005


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:pass@192.168.1.1:3128"

Thanks in advance, 
Ray




More information about the Python-list mailing list