ClientCookie

Michael Foord fuzzyman at gmail.com
Thu Aug 12 07:52:27 EDT 2004


fuzzyman at gmail.com (Michael Foord) wrote in message news:<6f402501.0408110018.1e4e5ca8 at posting.google.com>...
> 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
> 
> http://www.voidspace.org.uk/atlantibots/pythonutils.html

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

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))
ClientCookie.install_opener(opener)

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

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

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

print HR
print '<PRE>'
print info                     # print the headers
print
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
files...
*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)

Great

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html



More information about the Python-list mailing list