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