CookieJar that can be loaded from and saved to a file.
Additional methods
save(filename=None, ignore_discard=False, ignore_expires=False) load(filename=None, ignore_discard=False, ignore_expires=False) revert(filename=None, ignore_discard=False, ignore_expires=False)
Additional public attributes
filename: filename for loading and saving cookies
Additional public readable attributes
delayload: request that cookies are lazily loaded from disk; 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 (in fact, only MSIECookieJar lazily loads cookies at the moment)
There are no implemented interfaces.
domain_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x41696360>
dots_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x41695480>
non_word_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x41690608>
quote_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x416953e0>
strict_domain_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x41695430>
add_cookie_header(request)
Add correct Cookie: header to request (urllib2.Request object).
The Cookie2 header is also added unless policy.hide_cookie2 is true.
The request object (usually a urllib2.Request instance) must support the methods get_full_url, get_host, get_type, has_header, get_header, header_items and add_unredirected_header, as documented by urllib2, and the port attribute (the port number). Actually, RequestUpgradeProcessor will automatically upgrade your Request object to one with has_header, get_header, header_items and add_unredirected_header, if it lacks those methods, for compatibility with pre-2.4 versions of urllib2.
clear(domain=None, path=None, name=None)
Clear some cookies.
Invoking this method without arguments will 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 path within that domain are removed. If given three arguments, then the cookie with the specified name, path and domain is removed.
Raises KeyError if no matching cookie exists.
clear_expired_cookies()
Discard all expired cookies.
You probably don't need to call this method: expired cookies are never sent back to the server (provided you're using DefaultCookiePolicy), this method is called by CookieJar itself every so often, and the save method won't save expired cookies anyway (unless you ask otherwise by passing a true ignore_expires argument).
clear_session_cookies()
Discard all session cookies.
Discards all cookies held by object which had either no Max-Age or Expires cookie-attribute or an explicit Discard cookie-attribute, or which otherwise have ended up with a true discard 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.
extract_cookies(response, request)
Extract cookies from response, where allowable given the request.
Look for allowable Set-Cookie: and Set-Cookie2: headers in the response object passed as argument. Any of these headers that are found are used to update the state of the object (subject to the policy.set_ok method's approval).
The response object (usually be the result of a call to
mechanize.urlopen, or similar) should support an info method, which
returns a mimetools.Message object (in fact, the mimetools.Message
object
may be any object that provides a getallmatchingheaders
method).
The request object (usually a urllib2.Request instance) must support the methods get_full_url and get_host, as documented by urllib2, and the port attribute (the port number). The request is used to set default values for cookie-attributes as well as for checking that the cookie is OK to be set.
load(filename=None, ignore_discard=False, ignore_expires=False)
Load cookies from a file.
Old cookies are kept unless overwritten by newly loaded ones.
Arguments are as for .save().
If filename is not specified, self.filename is used; if self.filename is None, ValueError is raised. The named file must be in the format understood by the class, or LoadError will be raised. This format will be identical to that written by the save method, unless the load format is not sufficiently well understood (as is the case for MSIECookieJar).
make_cookies(response, request)
Return sequence of Cookie objects extracted from response object.
See extract_cookies.__doc__ for the interfaces required of the response and request arguments.
revert(filename=None, ignore_discard=False, ignore_expires=False)
Clear all cookies and reload cookies from a saved file.
Raises LoadError (or IOError) if reversion is not successful; the object's state will not be altered if this happens.
save(filename=None, ignore_discard=False, ignore_expires=False)
Save cookies to a file.
filename: name of file in which to save cookies 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 its cookies. Saved cookies can be restored later using the load or revert methods. If filename is not specified, self.filename is used; if self.filename is None, ValueError is raised.
set_cookie(cookie)
Set a cookie, without checking whether or not it should be set.
cookie: mechanize.Cookie instance
set_cookie_if_ok(cookie, request)
Set a cookie if policy says it's OK to do so.
cookie: mechanize.Cookie instance request: see extract_cookies.__doc__ for the required interface
set_policy(policy)