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