Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Both using cookies and a proxy in Python with urllib2

I'm using urllib2 to interact with a webserver. For the specific problem I need to solve, I need to tunnel the traffic through a proxy. I managed to do that with a urllib2 'ProxyHandler'.

I also need to accept and send cookies. I managed to do that with a urllib2 'cookielib.LWPCookieJar()'.

The problem is that while they work individually, they don't work 'together'. The last opener that I add with 'urllib2.install_opener(opener)' is the one that will work.

Is is possible to have two active 'openers'? Or another way to solve this problem?

like image 420
Erik Avatar asked Feb 09 '12 13:02

Erik


1 Answers

Combine proxy handler and cookie processor in a single opener:

cj = cookielib.CookieJar()
opener = build_opener(ProxyHandler({'http': 'ip:port'}), HTTPCookieProcessor(cj))
like image 114
jfs Avatar answered Oct 30 '22 01:10

jfs