Proxy + cookie: urllib2? httplib2? Other?

Gilles Ganault nospam at nospam.com
Fri Nov 14 10:47:30 EST 2008


On Fri, 14 Nov 2008 16:03:13 +0100, Gilles Ganault <nospam at nospam.com>
wrote:
>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,



More information about the Python-list mailing list