blob: 250abde7bb68942289d09c3f3b3ee8ba045d1460 [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
168 The *response* object (usually the result of a call to :meth:`urllib2.urlopen`,
169 or similar) should support an :meth:`info` method, which returns an object with
170 a :meth:`getallmatchingheaders` method (usually a :class:`mimetools.Message`
171 instance).
172
173 The *request* object (usually a :class:`urllib2.Request` instance) must support
174 the methods :meth:`get_full_url`, :meth:`get_host`, :meth:`unverifiable`, and
175 :meth:`get_origin_req_host`, as documented by :mod:`urllib2`. The request is
176 used to set default values for cookie-attributes as well as for checking that
177 the cookie is allowed to be set.
178
179
180.. method:: CookieJar.set_policy(policy)
181
182 Set the :class:`CookiePolicy` instance to be used.
183
184
185.. method:: CookieJar.make_cookies(response, request)
186
187 Return sequence of :class:`Cookie` objects extracted from *response* object.
188
189 See the documentation for :meth:`extract_cookies` for the interfaces required of
190 the *response* and *request* arguments.
191
192
193.. method:: CookieJar.set_cookie_if_ok(cookie, request)
194
195 Set a :class:`Cookie` if policy says it's OK to do so.
196
197
198.. method:: CookieJar.set_cookie(cookie)
199
200 Set a :class:`Cookie`, without checking with policy to see whether or not it
201 should be set.
202
203
204.. method:: CookieJar.clear([domain[, path[, name]]])
205
206 Clear some cookies.
207
208 If invoked without arguments, clear all cookies. If given a single argument,
209 only cookies belonging to that *domain* will be removed. If given two arguments,
210 cookies belonging to the specified *domain* and URL *path* are removed. If
211 given three arguments, then the cookie with the specified *domain*, *path* and
212 *name* is removed.
213
214 Raises :exc:`KeyError` if no matching cookie exists.
215
216
217.. method:: CookieJar.clear_session_cookies()
218
219 Discard all session cookies.
220
221 Discards all contained cookies that have a true :attr:`discard` attribute
222 (usually because they had either no ``max-age`` or ``expires`` cookie-attribute,
223 or an explicit ``discard`` cookie-attribute). For interactive browsers, the end
224 of a session usually corresponds to closing the browser window.
225
226 Note that the :meth:`save` method won't save session cookies anyway, unless you
227 ask otherwise by passing a true *ignore_discard* argument.
228
229:class:`FileCookieJar` implements the following additional methods:
230
231
232.. method:: FileCookieJar.save(filename=None, ignore_discard=False, ignore_expires=False)
233
234 Save cookies to a file.
235
236 This base class raises :exc:`NotImplementedError`. Subclasses may leave this
237 method unimplemented.
238
239 *filename* is the name of file in which to save cookies. If *filename* is not
240 specified, :attr:`self.filename` is used (whose default is the value passed to
241 the constructor, if any); if :attr:`self.filename` is :const:`None`,
242 :exc:`ValueError` is raised.
243
244 *ignore_discard*: save even cookies set to be discarded. *ignore_expires*: save
245 even cookies that have expired
246
247 The file is overwritten if it already exists, thus wiping all the cookies it
248 contains. Saved cookies can be restored later using the :meth:`load` or
249 :meth:`revert` methods.
250
251
252.. method:: FileCookieJar.load(filename=None, ignore_discard=False, ignore_expires=False)
253
254 Load cookies from a file.
255
256 Old cookies are kept unless overwritten by newly loaded ones.
257
258 Arguments are as for :meth:`save`.
259
260 The named file must be in the format understood by the class, or
261 :exc:`LoadError` will be raised. Also, :exc:`IOError` may be raised, for
262 example if the file does not exist.
263
Georg Brandl116aa622007-08-15 14:28:22 +0000264
265.. method:: FileCookieJar.revert(filename=None, ignore_discard=False, ignore_expires=False)
266
267 Clear all cookies and reload cookies from a saved file.
268
269 :meth:`revert` can raise the same exceptions as :meth:`load`. If there is a
270 failure, the object's state will not be altered.
271
272:class:`FileCookieJar` instances have the following public attributes:
273
274
275.. attribute:: FileCookieJar.filename
276
277 Filename of default file in which to keep cookies. This attribute may be
278 assigned to.
279
280
281.. attribute:: FileCookieJar.delayload
282
283 If true, load cookies lazily from disk. This attribute should not be assigned
284 to. This is only a hint, since this only affects performance, not behaviour
285 (unless the cookies on disk are changing). A :class:`CookieJar` object may
286 ignore it. None of the :class:`FileCookieJar` classes included in the standard
287 library lazily loads cookies.
288
289
290.. _file-cookie-jar-classes:
291
292FileCookieJar subclasses and co-operation with web browsers
293-----------------------------------------------------------
294
295The following :class:`CookieJar` subclasses are provided for reading and writing
296. Further :class:`CookieJar` subclasses, including one that reads Microsoft
297Internet Explorer cookies, are available at
298http://wwwsearch.sf.net/ClientCookie/.
299
300
301.. class:: MozillaCookieJar(filename, delayload=None, policy=None)
302
303 A :class:`FileCookieJar` that can load from and save cookies to disk in the
304 Mozilla ``cookies.txt`` file format (which is also used by the Lynx and Netscape
305 browsers).
306
307 .. note::
308
309 This loses information about RFC 2965 cookies, and also about newer or
310 non-standard cookie-attributes such as ``port``.
311
312 .. warning::
313
314 Back up your cookies before saving if you have cookies whose loss / corruption
315 would be inconvenient (there are some subtleties which may lead to slight
316 changes in the file over a load / save round-trip).
317
318 Also note that cookies saved while Mozilla is running will get clobbered by
319 Mozilla.
320
321
322.. class:: LWPCookieJar(filename, delayload=None, policy=None)
323
324 A :class:`FileCookieJar` that can load from and save cookies to disk in format
325 compatible with the libwww-perl library's ``Set-Cookie3`` file format. This is
326 convenient if you want to store cookies in a human-readable file.
327
328
329.. _cookie-policy-objects:
330
331CookiePolicy Objects
332--------------------
333
334Objects implementing the :class:`CookiePolicy` interface have the following
335methods:
336
337
338.. method:: CookiePolicy.set_ok(cookie, request)
339
340 Return boolean value indicating whether cookie should be accepted from server.
341
Georg Brandl24420152008-05-26 16:32:26 +0000342 *cookie* is a :class:`Cookie` instance. *request* is an object
Georg Brandl116aa622007-08-15 14:28:22 +0000343 implementing the interface defined by the documentation for
344 :meth:`CookieJar.extract_cookies`.
345
346
347.. method:: CookiePolicy.return_ok(cookie, request)
348
349 Return boolean value indicating whether cookie should be returned to server.
350
Georg Brandl24420152008-05-26 16:32:26 +0000351 *cookie* is a :class:`Cookie` instance. *request* is an object
Georg Brandl116aa622007-08-15 14:28:22 +0000352 implementing the interface defined by the documentation for
353 :meth:`CookieJar.add_cookie_header`.
354
355
356.. method:: CookiePolicy.domain_return_ok(domain, request)
357
358 Return false if cookies should not be returned, given cookie domain.
359
360 This method is an optimization. It removes the need for checking every cookie
361 with a particular domain (which might involve reading many files). Returning
362 true from :meth:`domain_return_ok` and :meth:`path_return_ok` leaves all the
363 work to :meth:`return_ok`.
364
365 If :meth:`domain_return_ok` returns true for the cookie domain,
366 :meth:`path_return_ok` is called for the cookie path. Otherwise,
367 :meth:`path_return_ok` and :meth:`return_ok` are never called for that cookie
368 domain. If :meth:`path_return_ok` returns true, :meth:`return_ok` is called
369 with the :class:`Cookie` object itself for a full check. Otherwise,
370 :meth:`return_ok` is never called for that cookie path.
371
372 Note that :meth:`domain_return_ok` is called for every *cookie* domain, not just
373 for the *request* domain. For example, the function might be called with both
374 ``".example.com"`` and ``"www.example.com"`` if the request domain is
375 ``"www.example.com"``. The same goes for :meth:`path_return_ok`.
376
377 The *request* argument is as documented for :meth:`return_ok`.
378
379
380.. method:: CookiePolicy.path_return_ok(path, request)
381
382 Return false if cookies should not be returned, given cookie path.
383
384 See the documentation for :meth:`domain_return_ok`.
385
386In addition to implementing the methods above, implementations of the
387:class:`CookiePolicy` interface must also supply the following attributes,
388indicating which protocols should be used, and how. All of these attributes may
389be assigned to.
390
391
392.. attribute:: CookiePolicy.netscape
393
394 Implement Netscape protocol.
395
396
397.. attribute:: CookiePolicy.rfc2965
398
399 Implement RFC 2965 protocol.
400
401
402.. attribute:: CookiePolicy.hide_cookie2
403
404 Don't add :mailheader:`Cookie2` header to requests (the presence of this header
405 indicates to the server that we understand RFC 2965 cookies).
406
407The most useful way to define a :class:`CookiePolicy` class is by subclassing
408from :class:`DefaultCookiePolicy` and overriding some or all of the methods
409above. :class:`CookiePolicy` itself may be used as a 'null policy' to allow
410setting and receiving any and all cookies (this is unlikely to be useful).
411
412
413.. _default-cookie-policy-objects:
414
415DefaultCookiePolicy Objects
416---------------------------
417
418Implements the standard rules for accepting and returning cookies.
419
420Both RFC 2965 and Netscape cookies are covered. RFC 2965 handling is switched
421off by default.
422
423The easiest way to provide your own policy is to override this class and call
424its methods in your overridden implementations before adding your own additional
425checks::
426
Georg Brandl24420152008-05-26 16:32:26 +0000427 import http.cookiejar
428 class MyCookiePolicy(http.cookiejar.DefaultCookiePolicy):
Georg Brandl116aa622007-08-15 14:28:22 +0000429 def set_ok(self, cookie, request):
Georg Brandl24420152008-05-26 16:32:26 +0000430 if not http.cookiejar.DefaultCookiePolicy.set_ok(self, cookie, request):
Georg Brandl116aa622007-08-15 14:28:22 +0000431 return False
432 if i_dont_want_to_store_this_cookie(cookie):
433 return False
434 return True
435
436In addition to the features required to implement the :class:`CookiePolicy`
437interface, this class allows you to block and allow domains from setting and
438receiving cookies. There are also some strictness switches that allow you to
439tighten up the rather loose Netscape protocol rules a little bit (at the cost of
440blocking some benign cookies).
441
442A domain blacklist and whitelist is provided (both off by default). Only domains
443not in the blacklist and present in the whitelist (if the whitelist is active)
444participate in cookie setting and returning. Use the *blocked_domains*
445constructor argument, and :meth:`blocked_domains` and
446:meth:`set_blocked_domains` methods (and the corresponding argument and methods
447for *allowed_domains*). If you set a whitelist, you can turn it off again by
448setting it to :const:`None`.
449
450Domains in block or allow lists that do not start with a dot must equal the
451cookie domain to be matched. For example, ``"example.com"`` matches a blacklist
452entry of ``"example.com"``, but ``"www.example.com"`` does not. Domains that do
453start with a dot are matched by more specific domains too. For example, both
454``"www.example.com"`` and ``"www.coyote.example.com"`` match ``".example.com"``
455(but ``"example.com"`` itself does not). IP addresses are an exception, and
456must match exactly. For example, if blocked_domains contains ``"192.168.1.2"``
457and ``".168.1.2"``, 192.168.1.2 is blocked, but 193.168.1.2 is not.
458
459:class:`DefaultCookiePolicy` implements the following additional methods:
460
461
462.. method:: DefaultCookiePolicy.blocked_domains()
463
464 Return the sequence of blocked domains (as a tuple).
465
466
467.. method:: DefaultCookiePolicy.set_blocked_domains(blocked_domains)
468
469 Set the sequence of blocked domains.
470
471
472.. method:: DefaultCookiePolicy.is_blocked(domain)
473
474 Return whether *domain* is on the blacklist for setting or receiving cookies.
475
476
477.. method:: DefaultCookiePolicy.allowed_domains()
478
479 Return :const:`None`, or the sequence of allowed domains (as a tuple).
480
481
482.. method:: DefaultCookiePolicy.set_allowed_domains(allowed_domains)
483
484 Set the sequence of allowed domains, or :const:`None`.
485
486
487.. method:: DefaultCookiePolicy.is_not_allowed(domain)
488
489 Return whether *domain* is not on the whitelist for setting or receiving
490 cookies.
491
492:class:`DefaultCookiePolicy` instances have the following attributes, which are
493all initialised from the constructor arguments of the same name, and which may
494all be assigned to.
495
496
497.. attribute:: DefaultCookiePolicy.rfc2109_as_netscape
498
499 If true, request that the :class:`CookieJar` instance downgrade RFC 2109 cookies
500 (ie. cookies received in a :mailheader:`Set-Cookie` header with a version
501 cookie-attribute of 1) to Netscape cookies by setting the version attribute of
502 the :class:`Cookie` instance to 0. The default value is :const:`None`, in which
503 case RFC 2109 cookies are downgraded if and only if RFC 2965 handling is turned
504 off. Therefore, RFC 2109 cookies are downgraded by default.
505
Georg Brandl116aa622007-08-15 14:28:22 +0000506
507General strictness switches:
508
Georg Brandl116aa622007-08-15 14:28:22 +0000509.. attribute:: DefaultCookiePolicy.strict_domain
510
511 Don't allow sites to set two-component domains with country-code top-level
512 domains like ``.co.uk``, ``.gov.uk``, ``.co.nz``.etc. This is far from perfect
513 and isn't guaranteed to work!
514
Georg Brandl116aa622007-08-15 14:28:22 +0000515
Georg Brandl55ac8f02007-09-01 13:51:09 +0000516RFC 2965 protocol strictness switches:
Georg Brandl116aa622007-08-15 14:28:22 +0000517
518.. attribute:: DefaultCookiePolicy.strict_rfc2965_unverifiable
519
520 Follow RFC 2965 rules on unverifiable transactions (usually, an unverifiable
521 transaction is one resulting from a redirect or a request for an image hosted on
522 another site). If this is false, cookies are *never* blocked on the basis of
523 verifiability
524
Georg Brandl116aa622007-08-15 14:28:22 +0000525
Georg Brandl55ac8f02007-09-01 13:51:09 +0000526Netscape protocol strictness switches:
Georg Brandl116aa622007-08-15 14:28:22 +0000527
528.. attribute:: DefaultCookiePolicy.strict_ns_unverifiable
529
530 apply RFC 2965 rules on unverifiable transactions even to Netscape cookies
531
532
533.. attribute:: DefaultCookiePolicy.strict_ns_domain
534
535 Flags indicating how strict to be with domain-matching rules for Netscape
536 cookies. See below for acceptable values.
537
538
539.. attribute:: DefaultCookiePolicy.strict_ns_set_initial_dollar
540
541 Ignore cookies in Set-Cookie: headers that have names starting with ``'$'``.
542
543
544.. attribute:: DefaultCookiePolicy.strict_ns_set_path
545
546 Don't allow setting cookies whose path doesn't path-match request URI.
547
548:attr:`strict_ns_domain` is a collection of flags. Its value is constructed by
549or-ing together (for example, ``DomainStrictNoDots|DomainStrictNonDomain`` means
550both flags are set).
551
552
553.. attribute:: DefaultCookiePolicy.DomainStrictNoDots
554
555 When setting cookies, the 'host prefix' must not contain a dot (eg.
556 ``www.foo.bar.com`` can't set a cookie for ``.bar.com``, because ``www.foo``
557 contains a dot).
558
559
560.. attribute:: DefaultCookiePolicy.DomainStrictNonDomain
561
562 Cookies that did not explicitly specify a ``domain`` cookie-attribute can only
563 be returned to a domain equal to the domain that set the cookie (eg.
564 ``spam.example.com`` won't be returned cookies from ``example.com`` that had no
565 ``domain`` cookie-attribute).
566
567
568.. attribute:: DefaultCookiePolicy.DomainRFC2965Match
569
570 When setting cookies, require a full RFC 2965 domain-match.
571
572The following attributes are provided for convenience, and are the most useful
573combinations of the above flags:
574
575
576.. attribute:: DefaultCookiePolicy.DomainLiberal
577
578 Equivalent to 0 (ie. all of the above Netscape domain strictness flags switched
579 off).
580
581
582.. attribute:: DefaultCookiePolicy.DomainStrict
583
584 Equivalent to ``DomainStrictNoDots|DomainStrictNonDomain``.
585
586
Georg Brandl116aa622007-08-15 14:28:22 +0000587Cookie Objects
588--------------
589
590:class:`Cookie` instances have Python attributes roughly corresponding to the
591standard cookie-attributes specified in the various cookie standards. The
592correspondence is not one-to-one, because there are complicated rules for
593assigning default values, because the ``max-age`` and ``expires``
594cookie-attributes contain equivalent information, and because RFC 2109 cookies
Georg Brandl24420152008-05-26 16:32:26 +0000595may be 'downgraded' by :mod:`http.cookiejar` from version 1 to version 0 (Netscape)
Georg Brandl116aa622007-08-15 14:28:22 +0000596cookies.
597
598Assignment to these attributes should not be necessary other than in rare
599circumstances in a :class:`CookiePolicy` method. The class does not enforce
600internal consistency, so you should know what you're doing if you do that.
601
602
603.. attribute:: Cookie.version
604
605 Integer or :const:`None`. Netscape cookies have :attr:`version` 0. RFC 2965 and
606 RFC 2109 cookies have a ``version`` cookie-attribute of 1. However, note that
Georg Brandl24420152008-05-26 16:32:26 +0000607 :mod:`http.cookiejar` may 'downgrade' RFC 2109 cookies to Netscape cookies, in which
Georg Brandl116aa622007-08-15 14:28:22 +0000608 case :attr:`version` is 0.
609
610
611.. attribute:: Cookie.name
612
613 Cookie name (a string).
614
615
616.. attribute:: Cookie.value
617
618 Cookie value (a string), or :const:`None`.
619
620
621.. attribute:: Cookie.port
622
623 String representing a port or a set of ports (eg. '80', or '80,8080'), or
624 :const:`None`.
625
626
627.. attribute:: Cookie.path
628
629 Cookie path (a string, eg. ``'/acme/rocket_launchers'``).
630
631
632.. attribute:: Cookie.secure
633
634 True if cookie should only be returned over a secure connection.
635
636
637.. attribute:: Cookie.expires
638
639 Integer expiry date in seconds since epoch, or :const:`None`. See also the
640 :meth:`is_expired` method.
641
642
643.. attribute:: Cookie.discard
644
645 True if this is a session cookie.
646
647
648.. attribute:: Cookie.comment
649
650 String comment from the server explaining the function of this cookie, or
651 :const:`None`.
652
653
654.. attribute:: Cookie.comment_url
655
656 URL linking to a comment from the server explaining the function of this cookie,
657 or :const:`None`.
658
659
660.. attribute:: Cookie.rfc2109
661
662 True if this cookie was received as an RFC 2109 cookie (ie. the cookie
663 arrived in a :mailheader:`Set-Cookie` header, and the value of the Version
664 cookie-attribute in that header was 1). This attribute is provided because
Georg Brandl24420152008-05-26 16:32:26 +0000665 :mod:`http.cookiejar` may 'downgrade' RFC 2109 cookies to Netscape cookies, in
Georg Brandl116aa622007-08-15 14:28:22 +0000666 which case :attr:`version` is 0.
667
Georg Brandl116aa622007-08-15 14:28:22 +0000668
669.. attribute:: Cookie.port_specified
670
671 True if a port or set of ports was explicitly specified by the server (in the
672 :mailheader:`Set-Cookie` / :mailheader:`Set-Cookie2` header).
673
674
675.. attribute:: Cookie.domain_specified
676
677 True if a domain was explicitly specified by the server.
678
679
680.. attribute:: Cookie.domain_initial_dot
681
682 True if the domain explicitly specified by the server began with a dot
683 (``'.'``).
684
685Cookies may have additional non-standard cookie-attributes. These may be
686accessed using the following methods:
687
688
689.. method:: Cookie.has_nonstandard_attr(name)
690
691 Return true if cookie has the named cookie-attribute.
692
693
694.. method:: Cookie.get_nonstandard_attr(name, default=None)
695
696 If cookie has the named cookie-attribute, return its value. Otherwise, return
697 *default*.
698
699
700.. method:: Cookie.set_nonstandard_attr(name, value)
701
702 Set the value of the named cookie-attribute.
703
704The :class:`Cookie` class also defines the following method:
705
706
707.. method:: Cookie.is_expired([now=:const:`None`])
708
709 True if cookie has passed the time at which the server requested it should
710 expire. If *now* is given (in seconds since the epoch), return whether the
711 cookie has expired at the specified time.
712
713
Georg Brandl116aa622007-08-15 14:28:22 +0000714Examples
715--------
716
Georg Brandl24420152008-05-26 16:32:26 +0000717The first example shows the most common usage of :mod:`http.cookiejar`::
Georg Brandl116aa622007-08-15 14:28:22 +0000718
Georg Brandl24420152008-05-26 16:32:26 +0000719 import http.cookiejar, urllib2
720 cj = http.cookiejar.CookieJar()
Georg Brandl116aa622007-08-15 14:28:22 +0000721 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
722 r = opener.open("http://example.com/")
723
724This example illustrates how to open a URL using your Netscape, Mozilla, or Lynx
725cookies (assumes Unix/Netscape convention for location of the cookies file)::
726
Georg Brandl24420152008-05-26 16:32:26 +0000727 import os, http.cookiejar, urllib2
728 cj = http.cookiejar.MozillaCookieJar()
Georg Brandl116aa622007-08-15 14:28:22 +0000729 cj.load(os.path.join(os.environ["HOME"], ".netscape/cookies.txt"))
730 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
731 r = opener.open("http://example.com/")
732
733The next example illustrates the use of :class:`DefaultCookiePolicy`. Turn on
734RFC 2965 cookies, be more strict about domains when setting and returning
735Netscape cookies, and block some domains from setting cookies or having them
736returned::
737
738 import urllib2
Georg Brandl24420152008-05-26 16:32:26 +0000739 from http.cookiejar import CookieJar, DefaultCookiePolicy
Georg Brandl116aa622007-08-15 14:28:22 +0000740 policy = DefaultCookiePolicy(
741 rfc2965=True, strict_ns_domain=Policy.DomainStrict,
742 blocked_domains=["ads.net", ".ads.net"])
743 cj = CookieJar(policy)
744 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
745 r = opener.open("http://example.com/")
746