Author: John J. Lee <jjl@pobox.com>
Keywords: cookie,HTTP,Python,web,client,client-side,HTML,META,HTTP-EQUIV,Refresh
Date: 2005-02-20

# ClientCookie

Please read this note explaining the relationship between ClientCookie, `cookielib` and `urllib2`, and which to use when.

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` and `Refresh` handling, automatic adding of the `Referer` [sic] header, automatic observance of `robots.txt` and lazily-`seek()`able responses. These extras are implemented using an extension that makes it easier to add new functionality to `urllib2`. 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.

Python 2.0 or above is required, and `urllib2` is recommended. If you have 2.1 or above, you've already got a recent enough version of `urllib2`. For Python 2.0, you need the newer versions from Python 2.1 (available from the source distribution or Python CVS: urllib2.py). 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 here and the docstrings in the module source code.

Other than Gisle, particular thanks to Johnny Lee (MSIE Perl code) and Ronald Tschalar (advice on Netscape cookies).

## Notes about ClientCookie, `urllib2` and `cookielib`

Even if you're not using Python 2.4, please note the last of these points.

1. The cookie handling parts of ClientCookie are in Python 2.4 standard library as module `cookielib` and extensions to module `urllib2`.
2. ClientCookie works with Python 2.4.
3. For new code to run on Python 2.4, I recommend use of standard library modules `urllib2` and `cookielib` instead of ClientCookie. I recommend turning on RFC 2965 support to work around a bug in `cookielib` in Python 2.4.0.
4. Handler classes thst are missing from 2.4's `urllib2` (eg. `HTTPRefreshProcessor`) may be used with 2.4's `urllib2` (however, note the paragraph below). With any version of Python, parts of `urllib2` that are missing from ClientCookie (eg. `ProxyHandler`) may be used with ClientCookie, and `urllib2.Request` objects may be used with ClientCookie. IMPORTANT: For all other code, use ClientCookie exclusively: do NOT mix use of ClientCookie and `urllib2`!

Finally, note that, if you want to use `ClientCookie.RefreshProcessor` with Python 2.4's `urllib2`, you must also use `ClientCookie.HTTPRedirectHandler`.

## Download

All documentation (including these web pages) is included in the distribution.

To port your code from 0.4.x to 1.0.x, see here.

Stable release.

- ClientCookie-1.0.3.tar.gz
- ClientCookie-1_0_3.zip
- Change Log (included in distribution)
- Older versions.

Old release.

- ClientCookie-0.4.19.tar.gz
- ClientCookie-0_4_19.zip
- Change Log (included in distribution)
- Older versions.

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

## FAQs - pre-install

- Doesn't the standard Python library module, `Cookie`, do this?

  No: Cookie.py does the server end of the job. It doesn't know when to accept cookies from a server or when to pass them back.

- Which version of Python do I need?

  2.0 or above.

- Is urllib2.py required?

  No. You probably want it, though.

- Which urllib2.py do I need?

  You don't, but if you want to use the extended `urllib2` callables from ClientCookie, and you have Python 2.0, you need to upgrade to the version from Python 2.1. Otherwise, you're OK.

- Which license?

  The BSD license (included in distribution).

- Where can I find out more about the HTTP cookie protocol?

  There is more than one protocol, in fact (see the docs for a brief explanation of the history):

  - The original Netscape cookie protocol - the standard still in use today, in theory (in reality, the protocol implemented by all the major browsers only bears a passing resemblance to the protocol sketched out in this document).
  - RFC 2109 - obsoleted by RFC 2965.
  - RFC 2965 - the Netscape protocol with the bugs fixed (not widely used - the Netscape protocol still dominates, and seems likely to remain dominant indefinitely, at least on the Internet). RFC 2964 discusses use of the protocol. Errata to RFC 2965 are currently being discussed on the http-state mailing list (update: list traffic died months ago and hasn't revived).
  - A paper by David Kristol setting out the history of the cookie standards in exhausting detail.
  - HTTP cookies FAQ.
- Which protocols does ClientCookie support?

  Netscape and RFC 2965. RFC 2965 handling is switched off by default.

- What about RFC 2109?

  RFC 2109 cookies are currently parsed as Netscape cookies, and treated by default as RFC 2965 cookies thereafter if RFC 2965 handling is enabled, or as Netscape cookies otherwise. RFC 2109 is officially obsoleted by RFC 2965. Browsers do use a few RFC 2109 features in their Netscape cookie implementations (`port` and `max-age`), and ClientCookie knows about that, too.

John J. Lee, February 2005.