468,283 Members | 1,616 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,283 developers. It's quick & easy.

urllib2 Opener and Proxy/Authentication issues

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
1 3049
"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.

Similar topics

4 posts views Thread by O. Koch | last post: by
2 posts views Thread by Andre Bocchini | last post: by
4 posts views Thread by bmiras | last post: by
1 post views Thread by tomazi75-nospam(at)gmail.com | last post: by
6 posts views Thread by Alejandro Dubrovsky | last post: by
reply views Thread by Astan Chee | last post: by
6 posts views Thread by Jack | last post: by
1 post views Thread by Magnus.Moraberg | last post: by
1 post views Thread by Larry Hale | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.