By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,212 Members | 1,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,212 IT Pros & Developers. It's quick & easy.

urllib2 Opener and Proxy/Authentication issues

P: n/a
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:pa**@192.168.1.1:3128"

Thanks in advance,
Ray

Nov 8 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Ray Slakinski" <ra***********@gmail.com> writes:
[...]
ps: settings.GlobalProxySetting is defined as a string, for example:

non-authenticated proxy: "http://192.168.1.1:3128"
authenticated proxy: "http://user:pa**@192.168.1.1: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

Nov 8 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.