the following code fails to open the url, through a
proxy (it tries to connect directly, as if no proxy was
supplied)
#! /usr/bin/env python
import urllib2
if __name__ == "__main__":
h_prox = urllib2.ProxyHandler({'http' :
'http://10.4.4.123:3128'})
#opner = urllib2.build_opener(h_prox,
urllib2.HTTPHandler)
opner = urllib2.build_opener(h_prox)
urllib2.install_opener(opner)
urllib2.urlopen('http://www.python.org')
but it works if I add ' urllib2.HTTPHandler' in
build_opener call (commented line)
It seems that 'urllib2:build_opener' calls first
'urllib2:OpenerDirector.add_handler' with all default
handlers (including HTTPhandler when not supplied in
'build_opener'), and after the user supplied
handlers,so the proxy handler is linked in second
position in 'OpenerDirector.handle_open['http'],
whereas it should be first in the open chain.
I checked my theory with a quick and dirty hack in
'add_handler', to force instance of ProxyHandler to be
inserted first in the handler_open['http'] list (it
works), but I think a more apropriated fix shoud be
done in 'build_opener'
regards
|