Using a proxy with urllib2

Rob Wolfe rw at smsnet.pl
Sat Jan 12 07:16:02 EST 2008


"Jack" <nospam at invalid.com> writes:

>>> 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?
>

You don't have to create another opener if you only want to add
some handler. You can use `add_handler` method, e.g.:
opener.add_handler(urllib2.HTTPCookieProcessor(cj))

HTH,
Rob




More information about the Python-list mailing list