blob: 4c8be2917b22dab731f7eea50834d7a532fa0468 [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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: John J. Lee <jjl@pobox.com>
8.. sectionauthor:: John J. Lee <jjl@pobox.com>
9
Raymond Hettinger469271d2011-01-27 20:38:46 +000010**Source code:** :source:`Lib/http/cookiejar.py`
11
12--------------
Georg Brandl116aa622007-08-15 14:28:22 +000013
Georg Brandl24420152008-05-26 16:32:26 +000014The :mod:`http.cookiejar` module defines classes for automatic handling of HTTP
Georg Brandl116aa622007-08-15 14:28:22 +000015cookies. It is useful for accessing web sites that require small pieces of data
16-- :dfn:`cookies` -- to be set on the client machine by an HTTP response from a
17web server, and then returned to the server in later HTTP requests.
18
19Both the regular Netscape cookie protocol and the protocol defined by
20:rfc:`2965` are handled. RFC 2965 handling is switched off by default.
21:rfc:`2109` cookies are parsed as Netscape cookies and subsequently treated
22either as Netscape or RFC 2965 cookies according to the 'policy' in effect.
23Note that the great majority of cookies on the Internet are Netscape cookies.
Georg Brandl24420152008-05-26 16:32:26 +000024:mod:`http.cookiejar` attempts to follow the de-facto Netscape cookie protocol (which
Georg Brandl116aa622007-08-15 14:28:22 +000025differs substantially from that set out in the original Netscape specification),
26including taking note of the ``max-age`` and ``port`` cookie-attributes
27introduced with RFC 2965.
28
29.. note::
30
31 The various named parameters found in :mailheader:`Set-Cookie` and
32 :mailheader:`Set-Cookie2` headers (eg. ``domain`` and ``expires``) are
33 conventionally referred to as :dfn:`attributes`. To distinguish them from
34 Python attributes, the documentation for this module uses the term
35 :dfn:`cookie-attribute` instead.
36
37
38The module defines the following exception:
39
40
41.. exception:: LoadError
42
43 Instances of :class:`FileCookieJar` raise this exception on failure to load
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020044 cookies from a file. :exc:`LoadError` is a subclass of :exc:`OSError`.
45
46 .. versionchanged:: 3.3
47 LoadError was made a subclass of :exc:`OSError` instead of
48 :exc:`IOError`.
Georg Brandl116aa622007-08-15 14:28:22 +000049
50
51The following classes are provided:
52
53
54.. class:: CookieJar(policy=None)
55
56 *policy* is an object implementing the :class:`CookiePolicy` interface.
57
58 The :class:`CookieJar` class stores HTTP cookies. It extracts cookies from HTTP
59 requests, and returns them in HTTP responses. :class:`CookieJar` instances
60 automatically expire contained cookies when necessary. Subclasses are also
61 responsible for storing and retrieving cookies from a file or database.
62
63
64.. class:: FileCookieJar(filename, delayload=None, policy=None)
65
66 *policy* is an object implementing the :class:`CookiePolicy` interface. For the
67 other arguments, see the documentation for the corresponding attributes.
68
69 A :class:`CookieJar` which can load cookies from, and perhaps save cookies to, a
70 file on disk. Cookies are **NOT** loaded from the named file until either the
71 :meth:`load` or :meth:`revert` method is called. Subclasses of this class are
72 documented in section :ref:`file-cookie-jar-classes`.
73
74
75.. class:: CookiePolicy()
76
77 This class is responsible for deciding whether each cookie should be accepted
78 from / returned to the server.
79
80
Paul Bailey4c339972018-10-08 13:49:29 -050081.. 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, secure_protocols=("https", "wss") )
Georg Brandl116aa622007-08-15 14:28:22 +000082
83 Constructor arguments should be passed as keyword arguments only.
84 *blocked_domains* is a sequence of domain names that we never accept cookies
85 from, nor return cookies to. *allowed_domains* if not :const:`None`, this is a
Paul Bailey4c339972018-10-08 13:49:29 -050086 sequence of the only domains for which we accept and return cookies.
87 *secure_protocols* is a sequence of protocols for which secure cookies can be
88 added to. By default *https* and *wss* (secure websocket) are considered
89 secure protocols. For all other arguments, see the documentation for
90 :class:`CookiePolicy` and :class:`DefaultCookiePolicy` objects.
Georg Brandl116aa622007-08-15 14:28:22 +000091
92 :class:`DefaultCookiePolicy` implements the standard accept / reject rules for
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +030093 Netscape and :rfc:`2965` cookies. By default, :rfc:`2109` cookies (ie. cookies
Georg Brandl116aa622007-08-15 14:28:22 +000094 received in a :mailheader:`Set-Cookie` header with a version cookie-attribute of
95 1) are treated according to the RFC 2965 rules. However, if RFC 2965 handling
Serhiy Storchakafbc1c262013-11-29 12:17:13 +020096 is turned off or :attr:`rfc2109_as_netscape` is ``True``, RFC 2109 cookies are
Georg Brandl116aa622007-08-15 14:28:22 +000097 'downgraded' by the :class:`CookieJar` instance to Netscape cookies, by
98 setting the :attr:`version` attribute of the :class:`Cookie` instance to 0.
99 :class:`DefaultCookiePolicy` also provides some parameters to allow some
100 fine-tuning of policy.
101
102
103.. class:: Cookie()
104
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300105 This class represents Netscape, :rfc:`2109` and :rfc:`2965` cookies. It is not
Georg Brandl24420152008-05-26 16:32:26 +0000106 expected that users of :mod:`http.cookiejar` construct their own :class:`Cookie`
Georg Brandl116aa622007-08-15 14:28:22 +0000107 instances. Instead, if necessary, call :meth:`make_cookies` on a
108 :class:`CookieJar` instance.
109
110
111.. seealso::
112
Georg Brandl029986a2008-06-23 11:44:14 +0000113 Module :mod:`urllib.request`
Georg Brandl116aa622007-08-15 14:28:22 +0000114 URL opening with automatic cookie handling.
115
Georg Brandl24420152008-05-26 16:32:26 +0000116 Module :mod:`http.cookies`
Georg Brandl116aa622007-08-15 14:28:22 +0000117 HTTP cookie classes, principally useful for server-side code. The
Georg Brandl24420152008-05-26 16:32:26 +0000118 :mod:`http.cookiejar` and :mod:`http.cookies` modules do not depend on each
119 other.
Georg Brandl116aa622007-08-15 14:28:22 +0000120
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300121 https://curl.haxx.se/rfc/cookie_spec.html
Georg Brandl116aa622007-08-15 14:28:22 +0000122 The specification of the original Netscape cookie protocol. Though this is
123 still the dominant protocol, the 'Netscape cookie protocol' implemented by all
Georg Brandl24420152008-05-26 16:32:26 +0000124 the major browsers (and :mod:`http.cookiejar`) only bears a passing resemblance to
Georg Brandl116aa622007-08-15 14:28:22 +0000125 the one sketched out in ``cookie_spec.html``.
126
127 :rfc:`2109` - HTTP State Management Mechanism
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300128 Obsoleted by :rfc:`2965`. Uses :mailheader:`Set-Cookie` with version=1.
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130 :rfc:`2965` - HTTP State Management Mechanism
131 The Netscape protocol with the bugs fixed. Uses :mailheader:`Set-Cookie2` in
132 place of :mailheader:`Set-Cookie`. Not widely used.
133
134 http://kristol.org/cookie/errata.html
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300135 Unfinished errata to :rfc:`2965`.
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137 :rfc:`2964` - Use of HTTP State Management
138
139.. _cookie-jar-objects:
140
141CookieJar and FileCookieJar Objects
142-----------------------------------
143
Georg Brandl9afde1c2007-11-01 20:32:30 +0000144:class:`CookieJar` objects support the :term:`iterator` protocol for iterating over
Georg Brandl116aa622007-08-15 14:28:22 +0000145contained :class:`Cookie` objects.
146
147:class:`CookieJar` has the following methods:
148
149
150.. method:: CookieJar.add_cookie_header(request)
151
152 Add correct :mailheader:`Cookie` header to *request*.
153
154 If policy allows (ie. the :attr:`rfc2965` and :attr:`hide_cookie2` attributes of
155 the :class:`CookieJar`'s :class:`CookiePolicy` instance are true and false
156 respectively), the :mailheader:`Cookie2` header is also added when appropriate.
157
Georg Brandl029986a2008-06-23 11:44:14 +0000158 The *request* object (usually a :class:`urllib.request..Request` instance)
159 must support the methods :meth:`get_full_url`, :meth:`get_host`,
Senthil Kumaran05ec6ac2013-05-23 05:27:38 -0700160 :meth:`get_type`, :meth:`unverifiable`, :meth:`has_header`,
161 :meth:`get_header`, :meth:`header_items`, :meth:`add_unredirected_header`
162 and :attr:`origin_req_host` attribute as documented by
163 :mod:`urllib.request`.
164
165 .. versionchanged:: 3.3
166
167 *request* object needs :attr:`origin_req_host` attribute. Dependency on a
168 deprecated method :meth:`get_origin_req_host` has been removed.
Georg Brandl116aa622007-08-15 14:28:22 +0000169
170
171.. method:: CookieJar.extract_cookies(response, request)
172
173 Extract cookies from HTTP *response* and store them in the :class:`CookieJar`,
174 where allowed by policy.
175
176 The :class:`CookieJar` will look for allowable :mailheader:`Set-Cookie` and
177 :mailheader:`Set-Cookie2` headers in the *response* argument, and store cookies
178 as appropriate (subject to the :meth:`CookiePolicy.set_ok` method's approval).
179
Georg Brandl83e9f4c2008-06-12 18:52:31 +0000180 The *response* object (usually the result of a call to
Georg Brandl029986a2008-06-23 11:44:14 +0000181 :meth:`urllib.request.urlopen`, or similar) should support an :meth:`info`
Martin Panter7462b6492015-11-02 03:37:02 +0000182 method, which returns an :class:`email.message.Message` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000183
Georg Brandl029986a2008-06-23 11:44:14 +0000184 The *request* object (usually a :class:`urllib.request.Request` instance)
185 must support the methods :meth:`get_full_url`, :meth:`get_host`,
Senthil Kumaran05ec6ac2013-05-23 05:27:38 -0700186 :meth:`unverifiable`, and :attr:`origin_req_host` attribute, as documented
187 by :mod:`urllib.request`. The request is used to set default values for
Georg Brandl029986a2008-06-23 11:44:14 +0000188 cookie-attributes as well as for checking that the cookie is allowed to be
189 set.
Georg Brandl116aa622007-08-15 14:28:22 +0000190
Senthil Kumaran05ec6ac2013-05-23 05:27:38 -0700191 .. versionchanged:: 3.3
192
193 *request* object needs :attr:`origin_req_host` attribute. Dependency on a
194 deprecated method :meth:`get_origin_req_host` has been removed.
Georg Brandl116aa622007-08-15 14:28:22 +0000195
196.. method:: CookieJar.set_policy(policy)
197
198 Set the :class:`CookiePolicy` instance to be used.
199
200
201.. method:: CookieJar.make_cookies(response, request)
202
203 Return sequence of :class:`Cookie` objects extracted from *response* object.
204
205 See the documentation for :meth:`extract_cookies` for the interfaces required of
206 the *response* and *request* arguments.
207
208
209.. method:: CookieJar.set_cookie_if_ok(cookie, request)
210
211 Set a :class:`Cookie` if policy says it's OK to do so.
212
213
214.. method:: CookieJar.set_cookie(cookie)
215
216 Set a :class:`Cookie`, without checking with policy to see whether or not it
217 should be set.
218
219
220.. method:: CookieJar.clear([domain[, path[, name]]])
221
222 Clear some cookies.
223
224 If invoked without arguments, clear all cookies. If given a single argument,
225 only cookies belonging to that *domain* will be removed. If given two arguments,
226 cookies belonging to the specified *domain* and URL *path* are removed. If
227 given three arguments, then the cookie with the specified *domain*, *path* and
228 *name* is removed.
229
230 Raises :exc:`KeyError` if no matching cookie exists.
231
232
233.. method:: CookieJar.clear_session_cookies()
234
235 Discard all session cookies.
236
237 Discards all contained cookies that have a true :attr:`discard` attribute
238 (usually because they had either no ``max-age`` or ``expires`` cookie-attribute,
239 or an explicit ``discard`` cookie-attribute). For interactive browsers, the end
240 of a session usually corresponds to closing the browser window.
241
242 Note that the :meth:`save` method won't save session cookies anyway, unless you
243 ask otherwise by passing a true *ignore_discard* argument.
244
245:class:`FileCookieJar` implements the following additional methods:
246
247
248.. method:: FileCookieJar.save(filename=None, ignore_discard=False, ignore_expires=False)
249
250 Save cookies to a file.
251
252 This base class raises :exc:`NotImplementedError`. Subclasses may leave this
253 method unimplemented.
254
255 *filename* is the name of file in which to save cookies. If *filename* is not
256 specified, :attr:`self.filename` is used (whose default is the value passed to
257 the constructor, if any); if :attr:`self.filename` is :const:`None`,
258 :exc:`ValueError` is raised.
259
260 *ignore_discard*: save even cookies set to be discarded. *ignore_expires*: save
261 even cookies that have expired
262
263 The file is overwritten if it already exists, thus wiping all the cookies it
264 contains. Saved cookies can be restored later using the :meth:`load` or
265 :meth:`revert` methods.
266
267
268.. method:: FileCookieJar.load(filename=None, ignore_discard=False, ignore_expires=False)
269
270 Load cookies from a file.
271
272 Old cookies are kept unless overwritten by newly loaded ones.
273
274 Arguments are as for :meth:`save`.
275
276 The named file must be in the format understood by the class, or
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200277 :exc:`LoadError` will be raised. Also, :exc:`OSError` may be raised, for
Georg Brandl116aa622007-08-15 14:28:22 +0000278 example if the file does not exist.
279
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200280 .. versionchanged:: 3.3
281 :exc:`IOError` used to be raised, it is now an alias of :exc:`OSError`.
282
Georg Brandl116aa622007-08-15 14:28:22 +0000283
284.. method:: FileCookieJar.revert(filename=None, ignore_discard=False, ignore_expires=False)
285
286 Clear all cookies and reload cookies from a saved file.
287
288 :meth:`revert` can raise the same exceptions as :meth:`load`. If there is a
289 failure, the object's state will not be altered.
290
291:class:`FileCookieJar` instances have the following public attributes:
292
293
294.. attribute:: FileCookieJar.filename
295
296 Filename of default file in which to keep cookies. This attribute may be
297 assigned to.
298
299
300.. attribute:: FileCookieJar.delayload
301
302 If true, load cookies lazily from disk. This attribute should not be assigned
303 to. This is only a hint, since this only affects performance, not behaviour
304 (unless the cookies on disk are changing). A :class:`CookieJar` object may
305 ignore it. None of the :class:`FileCookieJar` classes included in the standard
306 library lazily loads cookies.
307
308
309.. _file-cookie-jar-classes:
310
311FileCookieJar subclasses and co-operation with web browsers
312-----------------------------------------------------------
313
Senthil Kumaranaba088e2010-07-11 05:01:52 +0000314The following :class:`CookieJar` subclasses are provided for reading and
Serhiy Storchakaa4d170d2013-12-23 18:20:51 +0200315writing.
Georg Brandl116aa622007-08-15 14:28:22 +0000316
317.. class:: MozillaCookieJar(filename, delayload=None, policy=None)
318
319 A :class:`FileCookieJar` that can load from and save cookies to disk in the
320 Mozilla ``cookies.txt`` file format (which is also used by the Lynx and Netscape
321 browsers).
322
323 .. note::
324
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300325 This loses information about :rfc:`2965` cookies, and also about newer or
Georg Brandl116aa622007-08-15 14:28:22 +0000326 non-standard cookie-attributes such as ``port``.
327
328 .. warning::
329
330 Back up your cookies before saving if you have cookies whose loss / corruption
331 would be inconvenient (there are some subtleties which may lead to slight
332 changes in the file over a load / save round-trip).
333
334 Also note that cookies saved while Mozilla is running will get clobbered by
335 Mozilla.
336
337
338.. class:: LWPCookieJar(filename, delayload=None, policy=None)
339
340 A :class:`FileCookieJar` that can load from and save cookies to disk in format
341 compatible with the libwww-perl library's ``Set-Cookie3`` file format. This is
342 convenient if you want to store cookies in a human-readable file.
343
344
345.. _cookie-policy-objects:
346
347CookiePolicy Objects
348--------------------
349
350Objects implementing the :class:`CookiePolicy` interface have the following
351methods:
352
353
354.. method:: CookiePolicy.set_ok(cookie, request)
355
356 Return boolean value indicating whether cookie should be accepted from server.
357
Georg Brandl24420152008-05-26 16:32:26 +0000358 *cookie* is a :class:`Cookie` instance. *request* is an object
Georg Brandl116aa622007-08-15 14:28:22 +0000359 implementing the interface defined by the documentation for
360 :meth:`CookieJar.extract_cookies`.
361
362
363.. method:: CookiePolicy.return_ok(cookie, request)
364
365 Return boolean value indicating whether cookie should be returned to server.
366
Georg Brandl24420152008-05-26 16:32:26 +0000367 *cookie* is a :class:`Cookie` instance. *request* is an object
Georg Brandl116aa622007-08-15 14:28:22 +0000368 implementing the interface defined by the documentation for
369 :meth:`CookieJar.add_cookie_header`.
370
371
372.. method:: CookiePolicy.domain_return_ok(domain, request)
373
374 Return false if cookies should not be returned, given cookie domain.
375
376 This method is an optimization. It removes the need for checking every cookie
377 with a particular domain (which might involve reading many files). Returning
378 true from :meth:`domain_return_ok` and :meth:`path_return_ok` leaves all the
379 work to :meth:`return_ok`.
380
381 If :meth:`domain_return_ok` returns true for the cookie domain,
382 :meth:`path_return_ok` is called for the cookie path. Otherwise,
383 :meth:`path_return_ok` and :meth:`return_ok` are never called for that cookie
384 domain. If :meth:`path_return_ok` returns true, :meth:`return_ok` is called
385 with the :class:`Cookie` object itself for a full check. Otherwise,
386 :meth:`return_ok` is never called for that cookie path.
387
388 Note that :meth:`domain_return_ok` is called for every *cookie* domain, not just
389 for the *request* domain. For example, the function might be called with both
390 ``".example.com"`` and ``"www.example.com"`` if the request domain is
391 ``"www.example.com"``. The same goes for :meth:`path_return_ok`.
392
393 The *request* argument is as documented for :meth:`return_ok`.
394
395
396.. method:: CookiePolicy.path_return_ok(path, request)
397
398 Return false if cookies should not be returned, given cookie path.
399
400 See the documentation for :meth:`domain_return_ok`.
401
402In addition to implementing the methods above, implementations of the
403:class:`CookiePolicy` interface must also supply the following attributes,
404indicating which protocols should be used, and how. All of these attributes may
405be assigned to.
406
407
408.. attribute:: CookiePolicy.netscape
409
410 Implement Netscape protocol.
411
412
413.. attribute:: CookiePolicy.rfc2965
414
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300415 Implement :rfc:`2965` protocol.
Georg Brandl116aa622007-08-15 14:28:22 +0000416
417
418.. attribute:: CookiePolicy.hide_cookie2
419
420 Don't add :mailheader:`Cookie2` header to requests (the presence of this header
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300421 indicates to the server that we understand :rfc:`2965` cookies).
Georg Brandl116aa622007-08-15 14:28:22 +0000422
423The most useful way to define a :class:`CookiePolicy` class is by subclassing
424from :class:`DefaultCookiePolicy` and overriding some or all of the methods
425above. :class:`CookiePolicy` itself may be used as a 'null policy' to allow
426setting and receiving any and all cookies (this is unlikely to be useful).
427
428
429.. _default-cookie-policy-objects:
430
431DefaultCookiePolicy Objects
432---------------------------
433
434Implements the standard rules for accepting and returning cookies.
435
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300436Both :rfc:`2965` and Netscape cookies are covered. RFC 2965 handling is switched
Georg Brandl116aa622007-08-15 14:28:22 +0000437off by default.
438
439The easiest way to provide your own policy is to override this class and call
440its methods in your overridden implementations before adding your own additional
441checks::
442
Georg Brandl24420152008-05-26 16:32:26 +0000443 import http.cookiejar
444 class MyCookiePolicy(http.cookiejar.DefaultCookiePolicy):
Georg Brandl116aa622007-08-15 14:28:22 +0000445 def set_ok(self, cookie, request):
Georg Brandl24420152008-05-26 16:32:26 +0000446 if not http.cookiejar.DefaultCookiePolicy.set_ok(self, cookie, request):
Georg Brandl116aa622007-08-15 14:28:22 +0000447 return False
448 if i_dont_want_to_store_this_cookie(cookie):
449 return False
450 return True
451
452In addition to the features required to implement the :class:`CookiePolicy`
453interface, this class allows you to block and allow domains from setting and
454receiving cookies. There are also some strictness switches that allow you to
455tighten up the rather loose Netscape protocol rules a little bit (at the cost of
456blocking some benign cookies).
457
458A domain blacklist and whitelist is provided (both off by default). Only domains
459not in the blacklist and present in the whitelist (if the whitelist is active)
460participate in cookie setting and returning. Use the *blocked_domains*
461constructor argument, and :meth:`blocked_domains` and
462:meth:`set_blocked_domains` methods (and the corresponding argument and methods
463for *allowed_domains*). If you set a whitelist, you can turn it off again by
464setting it to :const:`None`.
465
466Domains in block or allow lists that do not start with a dot must equal the
467cookie domain to be matched. For example, ``"example.com"`` matches a blacklist
468entry of ``"example.com"``, but ``"www.example.com"`` does not. Domains that do
469start with a dot are matched by more specific domains too. For example, both
470``"www.example.com"`` and ``"www.coyote.example.com"`` match ``".example.com"``
471(but ``"example.com"`` itself does not). IP addresses are an exception, and
472must match exactly. For example, if blocked_domains contains ``"192.168.1.2"``
473and ``".168.1.2"``, 192.168.1.2 is blocked, but 193.168.1.2 is not.
474
475:class:`DefaultCookiePolicy` implements the following additional methods:
476
477
478.. method:: DefaultCookiePolicy.blocked_domains()
479
480 Return the sequence of blocked domains (as a tuple).
481
482
483.. method:: DefaultCookiePolicy.set_blocked_domains(blocked_domains)
484
485 Set the sequence of blocked domains.
486
487
488.. method:: DefaultCookiePolicy.is_blocked(domain)
489
490 Return whether *domain* is on the blacklist for setting or receiving cookies.
491
492
493.. method:: DefaultCookiePolicy.allowed_domains()
494
495 Return :const:`None`, or the sequence of allowed domains (as a tuple).
496
497
498.. method:: DefaultCookiePolicy.set_allowed_domains(allowed_domains)
499
500 Set the sequence of allowed domains, or :const:`None`.
501
502
503.. method:: DefaultCookiePolicy.is_not_allowed(domain)
504
505 Return whether *domain* is not on the whitelist for setting or receiving
506 cookies.
507
508:class:`DefaultCookiePolicy` instances have the following attributes, which are
509all initialised from the constructor arguments of the same name, and which may
510all be assigned to.
511
512
513.. attribute:: DefaultCookiePolicy.rfc2109_as_netscape
514
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300515 If true, request that the :class:`CookieJar` instance downgrade :rfc:`2109` cookies
Georg Brandl116aa622007-08-15 14:28:22 +0000516 (ie. cookies received in a :mailheader:`Set-Cookie` header with a version
517 cookie-attribute of 1) to Netscape cookies by setting the version attribute of
518 the :class:`Cookie` instance to 0. The default value is :const:`None`, in which
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300519 case RFC 2109 cookies are downgraded if and only if :rfc:`2965` handling is turned
Georg Brandl116aa622007-08-15 14:28:22 +0000520 off. Therefore, RFC 2109 cookies are downgraded by default.
521
Georg Brandl116aa622007-08-15 14:28:22 +0000522
523General strictness switches:
524
Georg Brandl116aa622007-08-15 14:28:22 +0000525.. attribute:: DefaultCookiePolicy.strict_domain
526
527 Don't allow sites to set two-component domains with country-code top-level
528 domains like ``.co.uk``, ``.gov.uk``, ``.co.nz``.etc. This is far from perfect
529 and isn't guaranteed to work!
530
Georg Brandl116aa622007-08-15 14:28:22 +0000531
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300532:rfc:`2965` protocol strictness switches:
Georg Brandl116aa622007-08-15 14:28:22 +0000533
534.. attribute:: DefaultCookiePolicy.strict_rfc2965_unverifiable
535
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300536 Follow :rfc:`2965` rules on unverifiable transactions (usually, an unverifiable
Georg Brandl116aa622007-08-15 14:28:22 +0000537 transaction is one resulting from a redirect or a request for an image hosted on
538 another site). If this is false, cookies are *never* blocked on the basis of
539 verifiability
540
Georg Brandl116aa622007-08-15 14:28:22 +0000541
Georg Brandl55ac8f02007-09-01 13:51:09 +0000542Netscape protocol strictness switches:
Georg Brandl116aa622007-08-15 14:28:22 +0000543
544.. attribute:: DefaultCookiePolicy.strict_ns_unverifiable
545
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300546 Apply :rfc:`2965` rules on unverifiable transactions even to Netscape cookies.
Georg Brandl116aa622007-08-15 14:28:22 +0000547
548
549.. attribute:: DefaultCookiePolicy.strict_ns_domain
550
551 Flags indicating how strict to be with domain-matching rules for Netscape
552 cookies. See below for acceptable values.
553
554
555.. attribute:: DefaultCookiePolicy.strict_ns_set_initial_dollar
556
557 Ignore cookies in Set-Cookie: headers that have names starting with ``'$'``.
558
559
560.. attribute:: DefaultCookiePolicy.strict_ns_set_path
561
562 Don't allow setting cookies whose path doesn't path-match request URI.
563
564:attr:`strict_ns_domain` is a collection of flags. Its value is constructed by
565or-ing together (for example, ``DomainStrictNoDots|DomainStrictNonDomain`` means
566both flags are set).
567
568
569.. attribute:: DefaultCookiePolicy.DomainStrictNoDots
570
571 When setting cookies, the 'host prefix' must not contain a dot (eg.
572 ``www.foo.bar.com`` can't set a cookie for ``.bar.com``, because ``www.foo``
573 contains a dot).
574
575
576.. attribute:: DefaultCookiePolicy.DomainStrictNonDomain
577
578 Cookies that did not explicitly specify a ``domain`` cookie-attribute can only
579 be returned to a domain equal to the domain that set the cookie (eg.
580 ``spam.example.com`` won't be returned cookies from ``example.com`` that had no
581 ``domain`` cookie-attribute).
582
583
584.. attribute:: DefaultCookiePolicy.DomainRFC2965Match
585
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300586 When setting cookies, require a full :rfc:`2965` domain-match.
Georg Brandl116aa622007-08-15 14:28:22 +0000587
588The following attributes are provided for convenience, and are the most useful
589combinations of the above flags:
590
591
592.. attribute:: DefaultCookiePolicy.DomainLiberal
593
594 Equivalent to 0 (ie. all of the above Netscape domain strictness flags switched
595 off).
596
597
598.. attribute:: DefaultCookiePolicy.DomainStrict
599
600 Equivalent to ``DomainStrictNoDots|DomainStrictNonDomain``.
601
602
Georg Brandl116aa622007-08-15 14:28:22 +0000603Cookie Objects
604--------------
605
606:class:`Cookie` instances have Python attributes roughly corresponding to the
607standard cookie-attributes specified in the various cookie standards. The
608correspondence is not one-to-one, because there are complicated rules for
609assigning default values, because the ``max-age`` and ``expires``
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300610cookie-attributes contain equivalent information, and because :rfc:`2109` cookies
Georg Brandl24420152008-05-26 16:32:26 +0000611may be 'downgraded' by :mod:`http.cookiejar` from version 1 to version 0 (Netscape)
Georg Brandl116aa622007-08-15 14:28:22 +0000612cookies.
613
614Assignment to these attributes should not be necessary other than in rare
615circumstances in a :class:`CookiePolicy` method. The class does not enforce
616internal consistency, so you should know what you're doing if you do that.
617
618
619.. attribute:: Cookie.version
620
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300621 Integer or :const:`None`. Netscape cookies have :attr:`version` 0. :rfc:`2965` and
622 :rfc:`2109` cookies have a ``version`` cookie-attribute of 1. However, note that
Georg Brandl24420152008-05-26 16:32:26 +0000623 :mod:`http.cookiejar` may 'downgrade' RFC 2109 cookies to Netscape cookies, in which
Georg Brandl116aa622007-08-15 14:28:22 +0000624 case :attr:`version` is 0.
625
626
627.. attribute:: Cookie.name
628
629 Cookie name (a string).
630
631
632.. attribute:: Cookie.value
633
634 Cookie value (a string), or :const:`None`.
635
636
637.. attribute:: Cookie.port
638
639 String representing a port or a set of ports (eg. '80', or '80,8080'), or
640 :const:`None`.
641
642
643.. attribute:: Cookie.path
644
645 Cookie path (a string, eg. ``'/acme/rocket_launchers'``).
646
647
648.. attribute:: Cookie.secure
649
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200650 ``True`` if cookie should only be returned over a secure connection.
Georg Brandl116aa622007-08-15 14:28:22 +0000651
652
653.. attribute:: Cookie.expires
654
655 Integer expiry date in seconds since epoch, or :const:`None`. See also the
656 :meth:`is_expired` method.
657
658
659.. attribute:: Cookie.discard
660
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200661 ``True`` if this is a session cookie.
Georg Brandl116aa622007-08-15 14:28:22 +0000662
663
664.. attribute:: Cookie.comment
665
666 String comment from the server explaining the function of this cookie, or
667 :const:`None`.
668
669
670.. attribute:: Cookie.comment_url
671
672 URL linking to a comment from the server explaining the function of this cookie,
673 or :const:`None`.
674
675
676.. attribute:: Cookie.rfc2109
677
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300678 ``True`` if this cookie was received as an :rfc:`2109` cookie (ie. the cookie
Georg Brandl116aa622007-08-15 14:28:22 +0000679 arrived in a :mailheader:`Set-Cookie` header, and the value of the Version
680 cookie-attribute in that header was 1). This attribute is provided because
Georg Brandl24420152008-05-26 16:32:26 +0000681 :mod:`http.cookiejar` may 'downgrade' RFC 2109 cookies to Netscape cookies, in
Georg Brandl116aa622007-08-15 14:28:22 +0000682 which case :attr:`version` is 0.
683
Georg Brandl116aa622007-08-15 14:28:22 +0000684
685.. attribute:: Cookie.port_specified
686
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200687 ``True`` if a port or set of ports was explicitly specified by the server (in the
Georg Brandl116aa622007-08-15 14:28:22 +0000688 :mailheader:`Set-Cookie` / :mailheader:`Set-Cookie2` header).
689
690
691.. attribute:: Cookie.domain_specified
692
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200693 ``True`` if a domain was explicitly specified by the server.
Georg Brandl116aa622007-08-15 14:28:22 +0000694
695
696.. attribute:: Cookie.domain_initial_dot
697
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200698 ``True`` if the domain explicitly specified by the server began with a dot
Georg Brandl116aa622007-08-15 14:28:22 +0000699 (``'.'``).
700
701Cookies may have additional non-standard cookie-attributes. These may be
702accessed using the following methods:
703
704
705.. method:: Cookie.has_nonstandard_attr(name)
706
707 Return true if cookie has the named cookie-attribute.
708
709
710.. method:: Cookie.get_nonstandard_attr(name, default=None)
711
712 If cookie has the named cookie-attribute, return its value. Otherwise, return
713 *default*.
714
715
716.. method:: Cookie.set_nonstandard_attr(name, value)
717
718 Set the value of the named cookie-attribute.
719
720The :class:`Cookie` class also defines the following method:
721
722
Hynek Schlawackdfa46522012-05-21 11:01:54 +0200723.. method:: Cookie.is_expired(now=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000724
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200725 ``True`` if cookie has passed the time at which the server requested it should
Georg Brandl116aa622007-08-15 14:28:22 +0000726 expire. If *now* is given (in seconds since the epoch), return whether the
727 cookie has expired at the specified time.
728
729
Georg Brandl116aa622007-08-15 14:28:22 +0000730Examples
731--------
732
Georg Brandl24420152008-05-26 16:32:26 +0000733The first example shows the most common usage of :mod:`http.cookiejar`::
Georg Brandl116aa622007-08-15 14:28:22 +0000734
Georg Brandl029986a2008-06-23 11:44:14 +0000735 import http.cookiejar, urllib.request
Georg Brandl24420152008-05-26 16:32:26 +0000736 cj = http.cookiejar.CookieJar()
Georg Brandl029986a2008-06-23 11:44:14 +0000737 opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
Georg Brandl116aa622007-08-15 14:28:22 +0000738 r = opener.open("http://example.com/")
739
740This example illustrates how to open a URL using your Netscape, Mozilla, or Lynx
741cookies (assumes Unix/Netscape convention for location of the cookies file)::
742
Georg Brandl029986a2008-06-23 11:44:14 +0000743 import os, http.cookiejar, urllib.request
Georg Brandl24420152008-05-26 16:32:26 +0000744 cj = http.cookiejar.MozillaCookieJar()
Éric Araujo4dcf5022011-03-25 20:31:50 +0100745 cj.load(os.path.join(os.path.expanduser("~"), ".netscape", "cookies.txt"))
Georg Brandl029986a2008-06-23 11:44:14 +0000746 opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
Georg Brandl116aa622007-08-15 14:28:22 +0000747 r = opener.open("http://example.com/")
748
749The next example illustrates the use of :class:`DefaultCookiePolicy`. Turn on
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300750:rfc:`2965` cookies, be more strict about domains when setting and returning
Georg Brandl116aa622007-08-15 14:28:22 +0000751Netscape cookies, and block some domains from setting cookies or having them
752returned::
753
Georg Brandl029986a2008-06-23 11:44:14 +0000754 import urllib.request
Georg Brandl24420152008-05-26 16:32:26 +0000755 from http.cookiejar import CookieJar, DefaultCookiePolicy
Georg Brandl116aa622007-08-15 14:28:22 +0000756 policy = DefaultCookiePolicy(
757 rfc2965=True, strict_ns_domain=Policy.DomainStrict,
758 blocked_domains=["ads.net", ".ads.net"])
759 cj = CookieJar(policy)
Georg Brandl029986a2008-06-23 11:44:14 +0000760 opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
Georg Brandl116aa622007-08-15 14:28:22 +0000761 r = opener.open("http://example.com/")
762