11.20.1 CookieJar and FileCookieJar Objects
CookieJar objects support the iterator protocol for iterating over contained Cookie objects.
CookieJar has the following methods:
-
Add correct Cookie: header to request.
If policy allows (ie. the rfc2965 and hide_cookie2 attributes of the CookieJar's CookiePolicy instance are true and false respectively), the Cookie2: header is also added when appropriate.
The request object (usually a urllib2.Request instance) must support the methods get_full_url(), get_host(), get_type(), unverifiable(), get_origin_req_host(), has_header(), get_header(), header_items(), and add_unredirected_header(),as documented by urllib2.
-
Extract cookies from HTTP response and store them in the
CookieJar, where allowed by policy.
The CookieJar will look for allowable Set-Cookie: and Set-Cookie2: headers in the response argument, and store cookies as appropriate (subject to the CookiePolicy.set_ok() method's approval).
The response object (usually the result of a call to urllib2.urlopen(), or similar) should support an info() method, which returns an object with a getallmatchingheaders() method (usually a mimetools.Message instance).
The request object (usually a urllib2.Request instance) must support the methods get_full_url(), get_host(), unverifiable(), and get_origin_req_host(), as documented by urllib2. The request is used to set default values for cookie-attributes as well as for checking that the cookie is allowed to be set.
- Set the CookiePolicy instance to be used.
-
Return sequence of Cookie objects extracted from
response object.
See the documentation for extract_cookies for the interfaces required of the response and request arguments.
- Set a Cookie if policy says it's OK to do so.
- Set a Cookie, without checking with policy to see whether or not it should be set.
-
Clear some cookies.
If invoked without arguments, clear all cookies. If given a single argument, only cookies belonging to that domain will be removed. If given two arguments, cookies belonging to the specified domain and URL path are removed. If given three arguments, then the cookie with the specified domain, path and name is removed.
Raises KeyError if no matching cookie exists.
-
Discard all session cookies.
Discards all contained cookies that have a true discard attribute (usually because they had either no
max-age
orexpires
cookie-attribute, or an explicitdiscard
cookie-attribute). For interactive browsers, the end of a session usually corresponds to closing the browser window.Note that the save() method won't save session cookies anyway, unless you ask otherwise by passing a true ignore_discard argument.
FileCookieJar implements the following additional methods:
-
Save cookies to a file.
This base class raises NotImplementedError. Subclasses may leave this method unimplemented.
filename is the name of file in which to save cookies. If filename is not specified, self.filename is used (whose default is the value passed to the constructor, if any); if self.filename is None, ValueError is raised.
ignore_discard: save even cookies set to be discarded. ignore_expires: save even cookies that have expired
The file is overwritten if it already exists, thus wiping all the cookies it contains. Saved cookies can be restored later using the load() or revert() methods.
-
Load cookies from a file.
Old cookies are kept unless overwritten by newly loaded ones.
Arguments are as for save().
The named file must be in the format understood by the class, or LoadError will be raised.
-
Clear all cookies and reload cookies from a saved file.
Raises cookielib.LoadError or IOError if reversion is not successful; the object's state will not be altered if this happens.
FileCookieJar instances have the following public attributes:
- Filename of default file in which to keep cookies. This attribute may be assigned to.
- If true, load cookies lazily from disk. This attribute should not be assigned to. This is only a hint, since this only affects performance, not behaviour (unless the cookies on disk are changing). A CookieJar object may ignore it. None of the FileCookieJar classes included in the standard library lazily loads cookies.
See About this document... for information on suggesting changes.