It's a BUG? urllib2 & proxy

limodou chatme at 263.net
Sun Oct 14 23:24:17 EDT 2001


I want to grab a web page through a firewall. So I used urllib2.py
module to do this. And the firewall needs a authentication. The code
is:

----------------------------------------
import urllib2

proxy=urllib2.ProxyHandler({'http':'http://user:passwd@11.11.11.11:8080'})
opener=urllib2.build_opener(proxy)
urllib2.install_opener(opener)
 
f = urllib2.urlopen('http://www.python.org/')

open('out.htm', 'w').write(f.read())
----------------------------------------

But the code failed.

Finally I find that in the source of urllib2.py, there are some codes:

    for klass in default_classes:
        opener.add_handler(klass())

    for h in handlers:
        if type(h) == types.ClassType:
            h = h()
        opener.add_handler(h)

I thought it is the key. So I move the first two lines backword, like
this:

#    for klass in default_classes:
#        opener.add_handler(klass())

    for h in handlers:
        if type(h) == types.ClassType:
            h = h()
        opener.add_handler(h)

    for klass in default_classes:
        opener.add_handler(klass())

It works.



More information about the Python-list mailing list