[cookielib] How to add cookies myself?

Gilles Ganault nospam at nospam.com
Tue Dec 16 15:41:38 CET 2008


I'm using urllib and urlib to download data from a web server that
requires cookies.

The issue I'm having, is the server uses JavaScript in the response to
insert new cookies and send them with the next query, so I need to
manually add a couple of cookies in the CookieJar, but I don't know
how to do this and Google didn't return examples:

import urllib
import urllib2
import cookielib

headers = {'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows
NT)'	}
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

url = "http://www.acme.com/index.php"
req = urllib2.Request(url, None, headers)
response = urllib2.urlopen(req).read()

print "Fetched cookies:"
for index, cookie in enumerate(cj):
	print index, '  :  ', cookie

How to Add manually?

Thanks for any tip.

More information about the Python-list mailing list