Using a proxy with urllib2

Jack nospam at
Thu Jan 10 05:45:34 CET 2008

I'm trying to use a proxy server with urllib2.
So I have managed to get it to work by setting the environment
export HTTP_PROXY=

But I wanted to set it from the code. However, this does not set the proxy:
    httpproxy = ''
    proxy_support = urllib2.ProxyHandler({"http":"http://" + httpproxy})
    opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
I'm using it from a URL handler file, not sure if it matters.

I have another question though. It seems that using either of the
methods above, the proxy will be global. What if I want to use
a proxy with one site, but not with another site? Or even use a
proxy for some URLs but not others? The proxy having to be global
is really not convenient. Is there any way to do per-fetch proxy? 

More information about the Python-list mailing list