Proxy + cookie: urllib2? httplib2? Other?

G

Gilles Ganault

Hello

I need a library that supports both going out through a proxy, and
handling cookies automagically (the server uses a sessionID to keep
track of the user).

UrlLib2 supports the proxy part, httplib2 supports the cookie part
but... Google didn't return code that shows both uses in the same
library. Does someone know if either of those, or yet some other
library would support both features?

Thank you.
 
G

Gilles Ganault

I need a library that supports both going out through a proxy, and
handling cookies automagically (the server uses a sessionID to keep
track of the user).

For those interested, it seems like a good combination is urllib2 +
cookielib:

==========
import urllib2
import cookielib

#Set up proxy infos
proxy_info = { 'host' : 'localhost','port' : 8119}
proxy_support = urllib2.ProxyHandler({"http" :
"http://%(host)s:%(port)d" % proxy_info})

#Set up cookie handler
cj = cookielib.CookieJar()

#Create UrlLib2 opener
opener =
urllib2.build_opener(proxy_support,urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)

headers = {'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows
NT)' }
url = 'http://www.acme.com'
req = urllib2.Request(url, None, headers)

response = urllib2.urlopen(req).read()

for index, cookie in enumerate(cj):
print index, ' : ', cookie
==========

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top