Michael Foord fuzzyman at
Thu Aug 12 13:52:27 CEST 2004

fuzzyman at (Michael Foord) wrote in message news:<6f402501.0408110018.1e4e5ca8 at>...
> Has anyone used ClientCookie to store cookies ? 
> I'm going to play around with 'pickling cookies' - but I wondered if
> anyone had any experience of this.
> (For session persistence in approx - my cgi proxy)
> Regards,
> Fuzzy

Ok, I've got somewhere.
I'm now using the 0.9 version and I found the debugging section in the

I'm using the example from the docs - but I'm pickling a list of the
pickles rather than using the load and save methods (as the docs
suggest) because I get an NotImplemented error for the save method (so
what use is the load method ?).

COOKIEFILE = 'cookies.pkl'
import ClientCookie
openfun = ClientCookie.urlopen

cj = ClientCookie.CookieJar()
opener = ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor(cj))

if os.path.isfile(COOKIEFILE):
    cookies = open(COOKIEFILE, 'rb')
    for entry in pickle.load(cookies):

cookies = open(COOKIEFILE, 'wb')
pickle.dump([c for c in cj], cookies)

req = ClientCookie.Request(theurl)
req.add_header('User-agent', 'Mozilla/4.0 (compatible; MSIE 5.5;
Windows NT)')
u = openfun(req)
info =

print HR
print '<PRE>'
print info                     # print the headers
print 'Cookies :'
a = 0
for c in cj:
    a += 1
    print a, c.__repr__()      # print each cookie
print '</PRE>'

and I'm now seeing persistent cookies..... hurrah.......
Can I save each cookie as a line of text ? Would that work with the
load method ?

Anyway - now I need to learn about server side cookies so that I can
give each user an 'id' and also write code to clean up unused cookie
*Then* I need to do a seperate CGI that will let users 'manage' their
cookies. (So I'll need to start understanding cookies a bit more)




More information about the Python-list mailing list