SourceForge.net Logo

ClientCookie

ClientCookie is a Python module for handling HTTP cookies on the client side, useful for accessing web sites that require cookies to be set and then returned later. It also provides some other (optional) useful stuff: HTTP-EQUIV handling, zero-time Refresh handling, and lazily-seekable responses. It has developed from a port of Gisle Aas' Perl module HTTP::Cookies, from the libwww-perl library.

 import ClientCookie
 response = ClientCookie.urlopen("http://foo.bar.com/")

This function behaves identically to urllib2.urlopen, except that it deals with cookies automatically. That's probably all you need to know.

Here is a more complicated example, involving Request objects (useful if you want to pass Requests around, add headers to them, etc.):

 import ClientCookie
 import urllib2
 request = urllib2.Request("http://www.acme.com/")
 # note we're using the urlopen from ClientCookie, not urllib2
 response = ClientCookie.urlopen(request)
 # let's say this next request requires a cookie that was set in response
 request2 = urllib2.Request("http://www.acme.com/flying_machines.html")
 response2 = ClientCookie.urlopen(request2)

In these examples, the workings are hidden inside the ClientCookie.urlopen method, which is an extension of urllib2.urlopen. Redirects, proxies and cookies are handled automatically by this function. Other, lower-level, cookie-aware extensions of urllib2 callables provided are: build_opener, install_opener, HTTPHandler and HTTPSHandler (if your Python installation has HTTPS support). A bugfixed HTTPRedirectHandler is also included (the bug, related to redirection, will be fixed in 2.3 final and 2.2.3).

An example at a slightly lower level shows what the module is doing more clearly:

 import ClientCookie
 import urllib2
 request = urllib2.Request("http://www.acme.com/")
 response = urllib2.urlopen(request)
 c = ClientCookie.Cookies()
 c.extract_cookies(response, request)
 # let's say this next request requires a cookie that was set in response
 request2 = urllib2.Request("http://www.acme.com/flying_machines.html")
 c.add_cookie_header(request2)
 response2 = urllib2.urlopen(request2)

 print response2.geturl()
 print response2.info()  # headers
 for line in response2.readlines():  # body
     print line

The Cookie class does all the work. There are essentially two operations: extract_cookies extracts HTTP cookies from Set-Cookie (the original Netscape cookie standard) and Set-Cookie2 (RFC 2965) headers from a response if and only if they should be set given the request, and add_cookie_header adds Cookie headers if and only if they are appropriate for a particular HTTP request. Incoming cookies are checked for acceptability based on the host name, etc. Cookies are only set on outgoing requests if they match the request's host name, path, etc. Cookies may be also be saved to and loaded from a file. The subclass NetscapeCookies differs from Cookies only in storing cookies using a different, Netscape-compatible, file format. This Netscape-compatible ('cookies.txt') format loses some information when you save cookies to a file.

Note that if you're using ClientCookie.urlopen (or ClientCookie.HTTPHandler or ClientCookie.HTTPSHandler), you don't need to call extract_cookies or add_cookie header yourself.

Python 1.5.2 or above is required, and urllib2 is recommended (for Python 1.5.2, use this urllib2 and this urllib). Note that the version of urllib2 from Python 2.0 is too old: if you have Python 2.0, get the version from Python 2.1 (available from the source distribution or CVS here from http://www.python.org/), or use the 1.5.2-compatible version. Note that you don't need to replace the original urllib2 / urllib - you can just make sure they're in sys.path ahead of the copies from 2.0's standard library.

For full documentation, see the docstrings in ClientCookie.py.

Download

This is a beta release (will be some time before I consider it stable, but it's quite useable as it is).

Development release. This is an alpha release: interfaces will change, and it's quite likely to be broken.

For installation instructions, see the INSTALL file included in the distribution.

FAQs - pre-install

FAQs - usage

John J. Lee, May 2003.