blob: b55da78c809ba44eb5ba997712fce1659bbe326b [file] [log] [blame]
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001\section{\module{cookielib} ---
2 Cookie handling for HTTP clients}
3
4\declaremodule{standard}{cookielib}
5\moduleauthor{John J. Lee}{jjl@pobox.com}
6\sectionauthor{John J. Lee}{jjl@pobox.com}
7
Andrew M. Kuchlingd54a0ae2005-12-04 20:25:23 +00008\versionadded{2.4}
9
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000010\modulesynopsis{Cookie handling for HTTP clients}
11
12The \module{cookielib} module defines classes for automatic handling
13of HTTP cookies. It is useful for accessing web sites that require
14small pieces of data -- \dfn{cookies} -- to be set on the client
15machine by an HTTP response from a web server, and then returned to
16the server in later HTTP requests.
17
18Both the regular Netscape cookie protocol and the protocol defined by
19\rfc{2965} are handled. RFC 2965 handling is switched off by default.
20\rfc{2109} cookies are parsed as Netscape cookies and subsequently
Neal Norwitz71dad722005-12-23 21:43:48 +000021treated either as Netscape or RFC 2965 cookies according to the
22'policy' in effect. Note that the great majority of cookies on the
23Internet are Netscape cookies. \module{cookielib} attempts to follow
24the de-facto Netscape cookie protocol (which differs substantially
25from that set out in the original Netscape specification), including
26taking note of the \code{max-age} and \code{port} cookie-attributes
27introduced with RFC 2109. \note{The various named parameters found in
28\mailheader{Set-Cookie} and \mailheader{Set-Cookie2} headers
29(eg. \code{domain} and \code{expires}) are conventionally referred to
30as \dfn{attributes}. To distinguish them from Python attributes, the
31documentation for this module uses the term \dfn{cookie-attribute}
32instead}.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000033
34
35The module defines the following exception:
36
37\begin{excdesc}{LoadError}
38Instances of \class{FileCookieJar} raise this exception on failure to
39load cookies from a file.
40\end{excdesc}
41
42
43The following classes are provided:
44
45\begin{classdesc}{CookieJar}{policy=\constant{None}}
46\var{policy} is an object implementing the \class{CookiePolicy}
47interface.
48
49The \class{CookieJar} class stores HTTP cookies. It extracts cookies
50from HTTP requests, and returns them in HTTP responses.
51\class{CookieJar} instances automatically expire contained cookies
52when necessary. Subclasses are also responsible for storing and
53retrieving cookies from a file or database.
54\end{classdesc}
55
56\begin{classdesc}{FileCookieJar}{filename, delayload=\constant{None},
57 policy=\constant{None}}
58\var{policy} is an object implementing the \class{CookiePolicy}
59interface. For the other arguments, see the documentation for the
60corresponding attributes.
61
62A \class{CookieJar} which can load cookies from, and perhaps save
63cookies to, a file on disk. Cookies are \strong{NOT} loaded from the
64named file until either the \method{load()} or \method{revert()}
65method is called. Subclasses of this class are documented in section
66\ref{file-cookie-jar-classes}.
67\end{classdesc}
68
69\begin{classdesc}{CookiePolicy}{}
70This class is responsible for deciding whether each cookie should be
71accepted from / returned to the server.
72\end{classdesc}
73
74\begin{classdesc}{DefaultCookiePolicy}{
75 blocked_domains=\constant{None},
76 allowed_domains=\constant{None},
77 netscape=\constant{True}, rfc2965=\constant{False},
Neal Norwitz71dad722005-12-23 21:43:48 +000078 rfc2109_as_netscape=\constant{None},
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000079 hide_cookie2=\constant{False},
80 strict_domain=\constant{False},
81 strict_rfc2965_unverifiable=\constant{True},
82 strict_ns_unverifiable=\constant{False},
83 strict_ns_domain=\constant{DefaultCookiePolicy.DomainLiberal},
84 strict_ns_set_initial_dollar=\constant{False},
85 strict_ns_set_path=\constant{False}
86 }
87
88Constructor arguments should be passed as keyword arguments only.
89\var{blocked_domains} is a sequence of domain names that we never
90accept cookies from, nor return cookies to. \var{allowed_domains} if
91not \constant{None}, this is a sequence of the only domains for which
92we accept and return cookies. For all other arguments, see the
93documentation for \class{CookiePolicy} and \class{DefaultCookiePolicy}
94objects.
95
96\class{DefaultCookiePolicy} implements the standard accept / reject
Neal Norwitz71dad722005-12-23 21:43:48 +000097rules for Netscape and RFC 2965 cookies. By default, RFC 2109 cookies
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000098(ie. cookies received in a \mailheader{Set-Cookie} header with a
99version cookie-attribute of 1) are treated according to the RFC 2965
Neal Norwitz71dad722005-12-23 21:43:48 +0000100rules. However, if RFC 2965 handling is turned off or
101\member{rfc2109_as_netscape} is True, RFC 2109 cookies are
102'downgraded' by the \class{CookieJar} instance to Netscape cookies, by
103setting the \member{version} attribute of the \class{Cookie} instance
104to 0. \class{DefaultCookiePolicy} also provides some parameters to
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000105allow some fine-tuning of policy.
106\end{classdesc}
107
108\begin{classdesc}{Cookie}{}
109This class represents Netscape, RFC 2109 and RFC 2965 cookies. It is
110not expected that users of \module{cookielib} construct their own
111\class{Cookie} instances. Instead, if necessary, call
112\method{make_cookies()} on a \class{CookieJar} instance.
113\end{classdesc}
114
115\begin{seealso}
116
117\seemodule{urllib2}{URL opening with automatic cookie handling.}
118
119\seemodule{Cookie}{HTTP cookie classes, principally useful for
120server-side code. The \module{cookielib} and \module{Cookie} modules
121do not depend on each other.}
122
123\seeurl{http://wwwsearch.sf.net/ClientCookie/}{Extensions to this
124module, including a class for reading Microsoft Internet Explorer
125cookies on Windows.}
126
127\seeurl{http://www.netscape.com/newsref/std/cookie_spec.html}{The
128specification of the original Netscape cookie protocol. Though this
129is still the dominant protocol, the 'Netscape cookie protocol'
130implemented by all the major browsers (and \module{cookielib}) only
131bears a passing resemblance to the one sketched out in
132\code{cookie_spec.html}.}
133
134\seerfc{2109}{HTTP State Management Mechanism}{Obsoleted by RFC 2965.
135Uses \mailheader{Set-Cookie} with version=1.}
136
137\seerfc{2965}{HTTP State Management Mechanism}{The Netscape protocol
138with the bugs fixed. Uses \mailheader{Set-Cookie2} in place of
139\mailheader{Set-Cookie}. Not widely used.}
140
141\seeurl{http://kristol.org/cookie/errata.html}{Unfinished errata to
142RFC 2965.}
143
144\seerfc{2964}{Use of HTTP State Management}{}
145
146\end{seealso}
147
148
149\subsection{CookieJar and FileCookieJar Objects \label{cookie-jar-objects}}
150
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000151\class{CookieJar} objects support the iterator protocol for iterating
152over contained \class{Cookie} objects.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000153
154\class{CookieJar} has the following methods:
155
156\begin{methoddesc}[CookieJar]{add_cookie_header}{request}
157Add correct \mailheader{Cookie} header to \var{request}.
158
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000159If policy allows (ie. the \member{rfc2965} and \member{hide_cookie2}
160attributes of the \class{CookieJar}'s \class{CookiePolicy} instance
161are true and false respectively), the \mailheader{Cookie2} header is
162also added when appropriate.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000163
164The \var{request} object (usually a \class{urllib2.Request} instance)
165must support the methods \method{get_full_url()}, \method{get_host()},
166\method{get_type()}, \method{unverifiable()},
167\method{get_origin_req_host()}, \method{has_header()},
168\method{get_header()}, \method{header_items()}, and
169\method{add_unredirected_header()},as documented by \module{urllib2}.
170\end{methoddesc}
171
172\begin{methoddesc}[CookieJar]{extract_cookies}{response, request}
173Extract cookies from HTTP \var{response} and store them in the
174\class{CookieJar}, where allowed by policy.
175
176The \class{CookieJar} will look for allowable \mailheader{Set-Cookie}
177and \mailheader{Set-Cookie2} headers in the \var{response} argument,
178and store cookies as appropriate (subject to the
179\method{CookiePolicy.set_ok()} method's approval).
180
181The \var{response} object (usually the result of a call to
182\method{urllib2.urlopen()}, or similar) should support an
183\method{info()} method, which returns an object with a
184\method{getallmatchingheaders()} method (usually a
185\class{mimetools.Message} instance).
186
187The \var{request} object (usually a \class{urllib2.Request} instance)
188must support the methods \method{get_full_url()}, \method{get_host()},
189\method{unverifiable()}, and \method{get_origin_req_host()}, as
190documented by \module{urllib2}. The request is used to set default
191values for cookie-attributes as well as for checking that the cookie
192is allowed to be set.
193\end{methoddesc}
194
195\begin{methoddesc}[CookieJar]{set_policy}{policy}
196Set the \class{CookiePolicy} instance to be used.
197\end{methoddesc}
198
199\begin{methoddesc}[CookieJar]{make_cookies}{response, request}
200Return sequence of \class{Cookie} objects extracted from
201\var{response} object.
202
203See the documentation for \method{extract_cookies} for the interfaces
204required of the \var{response} and \var{request} arguments.
205\end{methoddesc}
206
207\begin{methoddesc}[CookieJar]{set_cookie_if_ok}{cookie, request}
208Set a \class{Cookie} if policy says it's OK to do so.
209\end{methoddesc}
210
211\begin{methoddesc}[CookieJar]{set_cookie}{cookie}
212Set a \class{Cookie}, without checking with policy to see whether or
213not it should be set.
214\end{methoddesc}
215
216\begin{methoddesc}[CookieJar]{clear}{\optional{domain\optional{,
217 path\optional{, name}}}}
218Clear some cookies.
219
220If invoked without arguments, clear all cookies. If given a single
221argument, only cookies belonging to that \var{domain} will be removed.
222If given two arguments, cookies belonging to the specified
223\var{domain} and URL \var{path} are removed. If given three
224arguments, then the cookie with the specified \var{domain}, \var{path}
225and \var{name} is removed.
226
227Raises \exception{KeyError} if no matching cookie exists.
228\end{methoddesc}
229
230\begin{methoddesc}[CookieJar]{clear_session_cookies}{}
231Discard all session cookies.
232
233Discards all contained cookies that have a true \member{discard}
234attribute (usually because they had either no \code{max-age} or
235\code{expires} cookie-attribute, or an explicit \code{discard}
236cookie-attribute). For interactive browsers, the end of a session
237usually corresponds to closing the browser window.
238
239Note that the \method{save()} method won't save session cookies
240anyway, unless you ask otherwise by passing a true
241\var{ignore_discard} argument.
242\end{methoddesc}
243
244\class{FileCookieJar} implements the following additional methods:
245
246\begin{methoddesc}[FileCookieJar]{save}{filename=\constant{None},
247 ignore_discard=\constant{False}, ignore_expires=\constant{False}}
248Save cookies to a file.
249
250This base class raises \class{NotImplementedError}. Subclasses may
251leave this method unimplemented.
252
253\var{filename} is the name of file in which to save cookies. If
254\var{filename} is not specified, \member{self.filename} is used (whose
255default is the value passed to the constructor, if any); if
256\member{self.filename} is \constant{None}, \exception{ValueError} is
257raised.
258
259\var{ignore_discard}: save even cookies set to be discarded.
260\var{ignore_expires}: save even cookies that have expired
261
262The file is overwritten if it already exists, thus wiping all the
263cookies it contains. Saved cookies can be restored later using the
264\method{load()} or \method{revert()} methods.
265\end{methoddesc}
266
267\begin{methoddesc}[FileCookieJar]{load}{filename=\constant{None},
268 ignore_discard=\constant{False}, ignore_expires=\constant{False}}
269Load cookies from a file.
270
271Old cookies are kept unless overwritten by newly loaded ones.
272
273Arguments are as for \method{save()}.
274
275The named file must be in the format understood by the class, or
276\exception{LoadError} will be raised.
277\end{methoddesc}
278
279\begin{methoddesc}[FileCookieJar]{revert}{filename=\constant{None},
280 ignore_discard=\constant{False}, ignore_expires=\constant{False}}
281Clear all cookies and reload cookies from a saved file.
282
283Raises \exception{cookielib.LoadError} or \exception{IOError} if
284reversion is not successful; the object's state will not be altered if
285this happens.
286\end{methoddesc}
287
288\class{FileCookieJar} instances have the following public attributes:
289
290\begin{memberdesc}{filename}
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000291Filename of default file in which to keep cookies. This attribute may
292be assigned to.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000293\end{memberdesc}
294
295\begin{memberdesc}{delayload}
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000296If true, load cookies lazily from disk. This attribute should not be
297assigned to. This is only a hint, since this only affects
298performance, not behaviour (unless the cookies on disk are changing).
299A \class{CookieJar} object may ignore it. None of the
300\class{FileCookieJar} classes included in the standard library lazily
301loads cookies.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000302\end{memberdesc}
303
304
305\subsection{FileCookieJar subclasses and co-operation with web browsers
306 \label{file-cookie-jar-classes}}
307
308The following \class{CookieJar} subclasses are provided for reading
309and writing . Further \class{CookieJar} subclasses, including one
310that reads Microsoft Internet Explorer cookies, are available at
311\url{http://wwwsearch.sf.net/ClientCookie/}.
312
313\begin{classdesc}{MozillaCookieJar}{filename, delayload=\constant{None},
314 policy=\constant{None}}
315A \class{FileCookieJar} that can load from and save cookies to disk in
316the Mozilla \code{cookies.txt} file format (which is also used by the
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000317Lynx and Netscape browsers). \note{This loses information about RFC
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00003182965 cookies, and also about newer or non-standard cookie-attributes
319such as \code{port}.}
320
321\warning{Back up your cookies before saving if you have cookies whose
322loss / corruption would be inconvenient (there are some subtleties
323which may lead to slight changes in the file over a load / save
324round-trip).}
325
326Also note that cookies saved while Mozilla is running will get
327clobbered by Mozilla.
328\end{classdesc}
329
330\begin{classdesc}{LWPCookieJar}{filename, delayload=\constant{None},
331 policy=\constant{None}}
332A \class{FileCookieJar} that can load from and save cookies to disk in
333format compatible with the libwww-perl library's \code{Set-Cookie3}
334file format. This is convenient if you want to store cookies in a
335human-readable file.
336\end{classdesc}
337
338
339\subsection{CookiePolicy Objects \label{cookie-policy-objects}}
340
341Objects implementing the \class{CookiePolicy} interface have the
342following methods:
343
344\begin{methoddesc}[CookiePolicy]{set_ok}{cookie, request}
345Return boolean value indicating whether cookie should be accepted from server.
346
347\var{cookie} is a \class{cookielib.Cookie} instance. \var{request} is
348an object implementing the interface defined by the documentation for
349\method{CookieJar.extract_cookies()}.
350\end{methoddesc}
351
352\begin{methoddesc}[CookiePolicy]{return_ok}{cookie, request}
353Return boolean value indicating whether cookie should be returned to server.
354
355\var{cookie} is a \class{cookielib.Cookie} instance. \var{request} is
356an object implementing the interface defined by the documentation for
357\method{CookieJar.add_cookie_header()}.
358\end{methoddesc}
359
360\begin{methoddesc}[CookiePolicy]{domain_return_ok}{domain, request}
361Return false if cookies should not be returned, given cookie domain.
362
363This method is an optimization. It removes the need for checking
364every cookie with a particular domain (which might involve reading
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000365many files). Returning true from \method{domain_return_ok()} and
366\method{path_return_ok()} leaves all the work to \method{return_ok()}.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000367
368If \method{domain_return_ok()} returns true for the cookie domain,
369\method{path_return_ok()} is called for the cookie path. Otherwise,
370\method{path_return_ok()} and \method{return_ok()} are never called
371for that cookie domain. If \method{path_return_ok()} returns true,
372\method{return_ok()} is called with the \class{Cookie} object itself
373for a full check. Otherwise, \method{return_ok()} is never called for
374that cookie path.
375
376Note that \method{domain_return_ok()} is called for every
377\emph{cookie} domain, not just for the \emph{request} domain. For
378example, the function might be called with both \code{".example.com"}
379and \code{"www.example.com"} if the request domain is
380\code{"www.example.com"}. The same goes for
381\method{path_return_ok()}.
382
383The \var{request} argument is as documented for \method{return_ok()}.
384\end{methoddesc}
385
386\begin{methoddesc}[CookiePolicy]{path_return_ok}{path, request}
387Return false if cookies should not be returned, given cookie path.
388
389See the documentation for \method{domain_return_ok()}.
390\end{methoddesc}
391
392
393In addition to implementing the methods above, implementations of the
394\class{CookiePolicy} interface must also supply the following
395attributes, indicating which protocols should be used, and how. All
396of these attributes may be assigned to.
397
398\begin{memberdesc}{netscape}
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000399Implement Netscape protocol.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000400\end{memberdesc}
401\begin{memberdesc}{rfc2965}
402Implement RFC 2965 protocol.
403\end{memberdesc}
404\begin{memberdesc}{hide_cookie2}
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000405Don't add \mailheader{Cookie2} header to requests (the presence of
406this header indicates to the server that we understand RFC 2965
407cookies).
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000408\end{memberdesc}
409
410The most useful way to define a \class{CookiePolicy} class is by
411subclassing from \class{DefaultCookiePolicy} and overriding some or
412all of the methods above. \class{CookiePolicy} itself may be used as
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000413a 'null policy' to allow setting and receiving any and all cookies
414(this is unlikely to be useful).
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000415
416
417\subsection{DefaultCookiePolicy Objects \label{default-cookie-policy-objects}}
418
419Implements the standard rules for accepting and returning cookies.
420
421Both RFC 2965 and Netscape cookies are covered. RFC 2965 handling is
422switched off by default.
423
424The easiest way to provide your own policy is to override this class
Raymond Hettinger68804312005-01-01 00:28:46 +0000425and call its methods in your overridden implementations before adding
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000426your own additional checks:
427
428\begin{verbatim}
429import cookielib
430class MyCookiePolicy(cookielib.DefaultCookiePolicy):
431 def set_ok(self, cookie, request):
432 if not cookielib.DefaultCookiePolicy.set_ok(self, cookie, request):
433 return False
434 if i_dont_want_to_store_this_cookie(cookie):
435 return False
436 return True
437\end{verbatim}
438
439In addition to the features required to implement the
440\class{CookiePolicy} interface, this class allows you to block and
441allow domains from setting and receiving cookies. There are also some
442strictness switches that allow you to tighten up the rather loose
443Netscape protocol rules a little bit (at the cost of blocking some
444benign cookies).
445
446A domain blacklist and whitelist is provided (both off by default).
447Only domains not in the blacklist and present in the whitelist (if the
448whitelist is active) participate in cookie setting and returning. Use
449the \var{blocked_domains} constructor argument, and
450\method{blocked_domains()} and \method{set_blocked_domains()} methods
451(and the corresponding argument and methods for
452\var{allowed_domains}). If you set a whitelist, you can turn it off
453again by setting it to \constant{None}.
454
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000455Domains in block or allow lists that do not start with a dot must
456equal the cookie domain to be matched. For example,
457\code{"example.com"} matches a blacklist entry of
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000458\code{"example.com"}, but \code{"www.example.com"} does not. Domains
459that do start with a dot are matched by more specific domains too.
460For example, both \code{"www.example.com"} and
461\code{"www.coyote.example.com"} match \code{".example.com"} (but
462\code{"example.com"} itself does not). IP addresses are an exception,
463and must match exactly. For example, if blocked_domains contains
464\code{"192.168.1.2"} and \code{".168.1.2"}, 192.168.1.2 is blocked,
465but 193.168.1.2 is not.
466
467\class{DefaultCookiePolicy} implements the following additional
468methods:
469
470\begin{methoddesc}[DefaultCookiePolicy]{blocked_domains}{}
471Return the sequence of blocked domains (as a tuple).
472\end{methoddesc}
473
474\begin{methoddesc}[DefaultCookiePolicy]{set_blocked_domains}
475 {blocked_domains}
476Set the sequence of blocked domains.
477\end{methoddesc}
478
479\begin{methoddesc}[DefaultCookiePolicy]{is_blocked}{domain}
480Return whether \var{domain} is on the blacklist for setting or
481receiving cookies.
482\end{methoddesc}
483
484\begin{methoddesc}[DefaultCookiePolicy]{allowed_domains}{}
485Return \constant{None}, or the sequence of allowed domains (as a tuple).
486\end{methoddesc}
487
488\begin{methoddesc}[DefaultCookiePolicy]{set_allowed_domains}
489 {allowed_domains}
490Set the sequence of allowed domains, or \constant{None}.
491\end{methoddesc}
492
493\begin{methoddesc}[DefaultCookiePolicy]{is_not_allowed}{domain}
494Return whether \var{domain} is not on the whitelist for setting or
495receiving cookies.
496\end{methoddesc}
497
498\class{DefaultCookiePolicy} instances have the following attributes,
499which are all initialised from the constructor arguments of the same
500name, and which may all be assigned to.
501
Neal Norwitz71dad722005-12-23 21:43:48 +0000502\begin{memberdesc}{rfc2109_as_netscape}
503If true, request that the \class{CookieJar} instance downgrade RFC
5042109 cookies (ie. cookies received in a \mailheader{Set-Cookie} header
505with a version cookie-attribute of 1) to Netscape cookies by setting
506the version attribute of the \class{Cookie} instance to 0. The
507default value is \constant{None}, in which case RFC 2109 cookies are
508downgraded if and only if RFC 2965 handling is turned off. Therefore,
509RFC 2109 cookies are downgraded by default.
510\versionadded{2.5}
511\end{memberdesc}
512
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000513General strictness switches:
514
515\begin{memberdesc}{strict_domain}
516Don't allow sites to set two-component domains with country-code
517top-level domains like \code{.co.uk}, \code{.gov.uk},
518\code{.co.nz}.etc. This is far from perfect and isn't guaranteed to
519work!
520\end{memberdesc}
521
522RFC 2965 protocol strictness switches:
523
524\begin{memberdesc}{strict_rfc2965_unverifiable}
525Follow RFC 2965 rules on unverifiable transactions (usually, an
526unverifiable transaction is one resulting from a redirect or a request
527for an image hosted on another site). If this is false, cookies are
528\emph{never} blocked on the basis of verifiability
529\end{memberdesc}
530
531Netscape protocol strictness switches:
532
533\begin{memberdesc}{strict_ns_unverifiable}
534apply RFC 2965 rules on unverifiable transactions even to Netscape
535cookies
536\end{memberdesc}
537\begin{memberdesc}{strict_ns_domain}
538Flags indicating how strict to be with domain-matching rules for
539Netscape cookies. See below for acceptable values.
540\end{memberdesc}
541\begin{memberdesc}{strict_ns_set_initial_dollar}
542Ignore cookies in Set-Cookie: headers that have names starting with
543\code{'\$'}.
544\end{memberdesc}
545\begin{memberdesc}{strict_ns_set_path}
546Don't allow setting cookies whose path doesn't path-match request URI.
547\end{memberdesc}
548
549\member{strict_ns_domain} is a collection of flags. Its value is
550constructed by or-ing together (for example,
551\code{DomainStrictNoDots|DomainStrictNonDomain} means both flags are
552set).
553
554\begin{memberdesc}{DomainStrictNoDots}
555When setting cookies, the 'host prefix' must not contain a dot
556(eg. \code{www.foo.bar.com} can't set a cookie for \code{.bar.com},
557because \code{www.foo} contains a dot).
558\end{memberdesc}
559\begin{memberdesc}{DomainStrictNonDomain}
560Cookies that did not explicitly specify a \code{domain}
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000561cookie-attribute can only be returned to a domain equal to the domain
562that set the cookie (eg. \code{spam.example.com} won't be returned
563cookies from \code{example.com} that had no \code{domain}
564cookie-attribute).
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000565\end{memberdesc}
566\begin{memberdesc}{DomainRFC2965Match}
567When setting cookies, require a full RFC 2965 domain-match.
568\end{memberdesc}
569
570The following attributes are provided for convenience, and are the
571most useful combinations of the above flags:
572
573\begin{memberdesc}{DomainLiberal}
574Equivalent to 0 (ie. all of the above Netscape domain strictness flags
575switched off).
576\end{memberdesc}
577\begin{memberdesc}{DomainStrict}
578Equivalent to \code{DomainStrictNoDots|DomainStrictNonDomain}.
579\end{memberdesc}
580
581
582\subsection{Cookie Objects \label{cookie-jar-objects}}
583
584\class{Cookie} instances have Python attributes roughly corresponding
585to the standard cookie-attributes specified in the various cookie
586standards. The correspondence is not one-to-one, because there are
Neal Norwitz71dad722005-12-23 21:43:48 +0000587complicated rules for assigning default values, because the
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000588\code{max-age} and \code{expires} cookie-attributes contain equivalent
Neal Norwitz71dad722005-12-23 21:43:48 +0000589information, and because RFC 2109 cookies may be 'downgraded' by
590\module{cookielib} from version 1 to version 0 (Netscape) cookies.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000591
592Assignment to these attributes should not be necessary other than in
593rare circumstances in a \class{CookiePolicy} method. The class does
594not enforce internal consistency, so you should know what you're
595doing if you do that.
596
597\begin{memberdesc}[Cookie]{version}
Neal Norwitz71dad722005-12-23 21:43:48 +0000598Integer or \constant{None}. Netscape cookies have \member{version} 0.
599RFC 2965 and RFC 2109 cookies have a \code{version} cookie-attribute
600of 1. However, note that \module{cookielib} may 'downgrade' RFC 2109
601cookies to Netscape cookies, in which case \member{version} is 0.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000602\end{memberdesc}
603\begin{memberdesc}[Cookie]{name}
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000604Cookie name (a string).
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000605\end{memberdesc}
606\begin{memberdesc}[Cookie]{value}
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000607Cookie value (a string), or \constant{None}.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000608\end{memberdesc}
609\begin{memberdesc}[Cookie]{port}
610String representing a port or a set of ports (eg. '80', or '80,8080'),
611or \constant{None}.
612\end{memberdesc}
613\begin{memberdesc}[Cookie]{path}
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000614Cookie path (a string, eg. \code{'/acme/rocket_launchers'}).
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000615\end{memberdesc}
616\begin{memberdesc}[Cookie]{secure}
617True if cookie should only be returned over a secure connection.
618\end{memberdesc}
619\begin{memberdesc}[Cookie]{expires}
620Integer expiry date in seconds since epoch, or \constant{None}. See
621also the \method{is_expired()} method.
622\end{memberdesc}
623\begin{memberdesc}[Cookie]{discard}
624True if this is a session cookie.
625\end{memberdesc}
626\begin{memberdesc}[Cookie]{comment}
627String comment from the server explaining the function of this cookie,
628or \constant{None}.
629\end{memberdesc}
630\begin{memberdesc}[Cookie]{comment_url}
631URL linking to a comment from the server explaining the function of
632this cookie, or \constant{None}.
633\end{memberdesc}
Neal Norwitz71dad722005-12-23 21:43:48 +0000634\begin{memberdesc}[Cookie]{rfc2109}
635True if this cookie was received as an RFC 2109 cookie (ie. the cookie
636arrived in a \mailheader{Set-Cookie} header, and the value of the
637Version cookie-attribute in that header was 1). This attribute is
638provided because \module{cookielib} may 'downgrade' RFC 2109 cookies
639to Netscape cookies, in which case \member{version} is 0.
640\versionadded{2.5}
641\end{memberdesc}
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000642
643\begin{memberdesc}[Cookie]{port_specified}
644True if a port or set of ports was explicitly specified by the server
645(in the \mailheader{Set-Cookie} / \mailheader{Set-Cookie2} header).
646\end{memberdesc}
647\begin{memberdesc}[Cookie]{domain_specified}
648True if a domain was explicitly specified by the server.
649\end{memberdesc}
650\begin{memberdesc}[Cookie]{domain_initial_dot}
651True if the domain explicitly specified by the server began with a
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000652dot (\code{'.'}).
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000653\end{memberdesc}
654
655Cookies may have additional non-standard cookie-attributes. These may
656be accessed using the following methods:
657
658\begin{methoddesc}[Cookie]{has_nonstandard_attr}{name}
659Return true if cookie has the named cookie-attribute.
660\end{methoddesc}
661\begin{methoddesc}[Cookie]{get_nonstandard_attr}{name, default=\constant{None}}
662If cookie has the named cookie-attribute, return its value.
663Otherwise, return \var{default}.
664\end{methoddesc}
665\begin{methoddesc}[Cookie]{set_nonstandard_attr}{name, value}
666Set the value of the named cookie-attribute.
667\end{methoddesc}
668
669The \class{Cookie} class also defines the following method:
670
671\begin{methoddesc}[Cookie]{is_expired}{\optional{now=\constant{None}}}
672True if cookie has passed the time at which the server requested it
673should expire. If \var{now} is given (in seconds since the epoch),
674return whether the cookie has expired at the specified time.
675\end{methoddesc}
676
677
678\subsection{Examples \label{cookielib-examples}}
679
680The first example shows the most common usage of \module{cookielib}:
681
682\begin{verbatim}
683import cookielib, urllib2
684cj = cookielib.CookieJar()
685opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
686r = opener.open("http://example.com/")
687\end{verbatim}
688
689This example illustrates how to open a URL using your Netscape,
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000690Mozilla, or Lynx cookies (assumes \UNIX{}/Netscape convention for
691location of the cookies file):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000692
693\begin{verbatim}
694import os, cookielib, urllib2
695cj = cookielib.MozillaCookieJar()
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000696cj.load(os.path.join(os.environ["HOME"], ".netscape/cookies.txt"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000697opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
698r = opener.open("http://example.com/")
699\end{verbatim}
700
701The next example illustrates the use of \class{DefaultCookiePolicy}.
702Turn on RFC 2965 cookies, be more strict about domains when setting
703and returning Netscape cookies, and block some domains from setting
704cookies or having them returned:
705
706\begin{verbatim}
707import urllib2
Andrew M. Kuchling3a2418a2004-07-10 18:41:28 +0000708from cookielib import CookieJar, DefaultCookiePolicy
709policy = DefaultCookiePolicy(
710 rfc2965=True, strict_ns_domain=Policy.DomainStrict,
711 blocked_domains=["ads.net", ".ads.net"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000712cj = CookieJar(policy)
713opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
714r = opener.open("http://example.com/")
715\end{verbatim}