Using a proxy with urllib2

Jack nospam at invalid.com
Sat Jan 12 01:07:07 EST 2008


>> I'm trying to use a proxy server with urllib2.
>> So I have managed to get it to work by setting the environment
>> variable:
>> export HTTP_PROXY=127.0.0.1:8081
>>
>> But I wanted to set it from the code. However, this does not set the 
>> proxy:
>>     httpproxy = '127.0.0.1:3129'
>>     proxy_support = urllib2.ProxyHandler({"http":"http://" + httpproxy})
>>     opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
>>     urllib2.install_opener(opener)

I find out why it doesn't work in my code but I don't have a solution - 
somewhere
else in the code calls these two lines:

    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)

and they override the proxy opener. Could anyone tell me how to use both 
openers?





More information about the Python-list mailing list