ClientCookie

Michael Foord fuzzyman at gmail.com
Thu Aug 12 05:08:15 EDT 2004


jjl at pobox.com (John J. Lee) wrote in message news:<87llglqp0l.fsf at pobox.com>...
> fuzzyman at gmail.com (Michael Foord) writes:
> 
> > 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.
> [...]
> 
> There shouldn't be any problem with that.  You can pickle the
> CookieJar itself, or the cookies inside it ([c for c in cookiejar] --
> and use .set_cookie() to get them back into a new CookieJar).
> 

At the moment I can't get at the cookies at *all*. Can you see what
I'm doing wrong.
Here's my code (simplified) :


import ClientCookie
openfun = ClientCookie.urlopen

cj = ClientCookie.CookieJar()
req = ClientCookie.Request(theurl, txdata, txheaders)
u = openfun(req)
info = u.info()

print '<PRE>'        # This is ina CGI after all :-)
print info           # This prints the headers from the server
print
print 'Cookies :'
print cj                
for c in cj:
    print c
print '</PRE>'


Now if I set theurl to 'http://www.google.co.uk' I get the following
response :
(txdata=None, txheaders={ 'User-agent': 'Mozilla/4.0 (compatible; MSIE
5.5; Windows NT)' })

<PRE>
Cache-Control: private
Content-Type: text/html
Set-Cookie: PREF=ID=0bac71b03c6b1aa8:LD=en:TM=1092300998:LM=1092300998:S=YasA-Kgirv2NPnd9;
expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.co.uk
Server: GWS/2.1
Content-Length: 2901
Date: Thu, 12 Aug 2004 08:56:38 GMT
X-Cache: MISS from dav-serv.tbsmerchants.co.uk
Proxy-Connection: close


Cookies :
<ClientCookie._ClientCookie.CookieJar[]>
</PRE>

And I'm getting this consistently - I can see a cookie in the header,
but it never appears in the CookieJar - so loading and saving the
CookieJar is of no avail.

I'm sure I'm making an obvious mistake - but short of subclassing the
CookieProcessor and doing it all manually (which seems overkill) - I'm
a bit stumped.


> May I suggest instead using cookielib, from Python CVS?  (note that
> POSTs with urllib2 are broken in 2.4a2, so don't use that)
> 
> cookielib is a new module in 2.4, and is a cleaned-up version of the
> cookie-handling parts of ClientCookie.
> 
> 

I'll have a look in a bit... once I can get any kind of response !!

Thanks for your help.

Fuzzy


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


> John



More information about the Python-list mailing list