blob: c8e72989446302b4b93bdaff595b48415221fdfc [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`cookielib` --- Cookie handling for HTTP clients
3=====================================================
4
5.. module:: cookielib
6 :synopsis: Classes for automatic handling of HTTP cookies.
7.. moduleauthor:: John J. Lee <jjl@pobox.com>
8.. sectionauthor:: John J. Lee <jjl@pobox.com>
9
10
Georg Brandl116aa622007-08-15 14:28:22 +000011The :mod:`cookielib` module defines classes for automatic handling of HTTP
12cookies. It is useful for accessing web sites that require small pieces of data
13-- :dfn:`cookies` -- to be set on the client machine by an HTTP response from a
14web server, and then returned to the server in later HTTP requests.
15
16Both the regular Netscape cookie protocol and the protocol defined by
17:rfc:`2965` are handled. RFC 2965 handling is switched off by default.
18:rfc:`2109` cookies are parsed as Netscape cookies and subsequently treated
19either as Netscape or RFC 2965 cookies according to the 'policy' in effect.
20Note that the great majority of cookies on the Internet are Netscape cookies.
21:mod:`cookielib` attempts to follow the de-facto Netscape cookie protocol (which
22differs substantially from that set out in the original Netscape specification),
23including taking note of the ``max-age`` and ``port`` cookie-attributes
24introduced with RFC 2965.
25
26.. note::
27
28 The various named parameters found in :mailheader:`Set-Cookie` and
29 :mailheader:`Set-Cookie2` headers (eg. ``domain`` and ``expires``) are
30 conventionally referred to as :dfn:`attributes`. To distinguish them from
31 Python attributes, the documentation for this module uses the term
32 :dfn:`cookie-attribute` instead.
33
34
35The module defines the following exception:
36
37
38.. exception:: LoadError
39
40 Instances of :class:`FileCookieJar` raise this exception on failure to load
Georg Brandle6bcc912008-05-12 18:05:20 +000041 cookies from a file. :exc:`LoadError` is a subclass of :exc:`IOError`.
Georg Brandl116aa622007-08-15 14:28:22 +000042
43
44The following classes are provided:
45
46
47.. class:: CookieJar(policy=None)
48
49 *policy* is an object implementing the :class:`CookiePolicy` interface.
50
51 The :class:`CookieJar` class stores HTTP cookies. It extracts cookies from HTTP
52 requests, and returns them in HTTP responses. :class:`CookieJar` instances
53 automatically expire contained cookies when necessary. Subclasses are also
54 responsible for storing and retrieving cookies from a file or database.
55
56
57.. class:: FileCookieJar(filename, delayload=None, policy=None)
58
59 *policy* is an object implementing the :class:`CookiePolicy` interface. For the
60 other arguments, see the documentation for the corresponding attributes.
61
62 A :class:`CookieJar` which can load cookies from, and perhaps save cookies to, a
63 file on disk. Cookies are **NOT** loaded from the named file until either the
64 :meth:`load` or :meth:`revert` method is called. Subclasses of this class are
65 documented in section :ref:`file-cookie-jar-classes`.
66
67
68.. class:: CookiePolicy()
69
70 This class is responsible for deciding whether each cookie should be accepted
71 from / returned to the server.
72
73
74.. 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 )
75
76 Constructor arguments should be passed as keyword arguments only.
77 *blocked_domains* is a sequence of domain names that we never accept cookies
78 from, nor return cookies to. *allowed_domains* if not :const:`None`, this is a
79 sequence of the only domains for which we accept and return cookies. For all
80 other arguments, see the documentation for :class:`CookiePolicy` and
81 :class:`DefaultCookiePolicy` objects.
82
83 :class:`DefaultCookiePolicy` implements the standard accept / reject rules for
84 Netscape and RFC 2965 cookies. By default, RFC 2109 cookies (ie. cookies
85 received in a :mailheader:`Set-Cookie` header with a version cookie-attribute of
86 1) are treated according to the RFC 2965 rules. However, if RFC 2965 handling
87 is turned off or :attr:`rfc2109_as_netscape` is True, RFC 2109 cookies are
88 'downgraded' by the :class:`CookieJar` instance to Netscape cookies, by
89 setting the :attr:`version` attribute of the :class:`Cookie` instance to 0.
90 :class:`DefaultCookiePolicy` also provides some parameters to allow some
91 fine-tuning of policy.
92
93
94.. class:: Cookie()
95
96 This class represents Netscape, RFC 2109 and RFC 2965 cookies. It is not
97 expected that users of :mod:`cookielib` construct their own :class:`Cookie`
98 instances. Instead, if necessary, call :meth:`make_cookies` on a
99 :class:`CookieJar` instance.
100
101
102.. seealso::
103
104 Module :mod:`urllib2`
105 URL opening with automatic cookie handling.
106
107 Module :mod:`Cookie`
108 HTTP cookie classes, principally useful for server-side code. The
109 :mod:`cookielib` and :mod:`Cookie` modules do not depend on each other.
110
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
118 the major browsers (and :mod:`cookielib`) only bears a passing resemblance to
119 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
342 *cookie* is a :class:`cookielib.Cookie` instance. *request* is an object
343 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
351 *cookie* is a :class:`cookielib.Cookie` instance. *request* is an object
352 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
427 import cookielib
428 class MyCookiePolicy(cookielib.DefaultCookiePolicy):
429 def set_ok(self, cookie, request):
430 if not cookielib.DefaultCookiePolicy.set_ok(self, cookie, request):
431 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
587.. _cookielib-cookie-objects:
588
589Cookie Objects
590--------------
591
592:class:`Cookie` instances have Python attributes roughly corresponding to the
593standard cookie-attributes specified in the various cookie standards. The
594correspondence is not one-to-one, because there are complicated rules for
595assigning default values, because the ``max-age`` and ``expires``
596cookie-attributes contain equivalent information, and because RFC 2109 cookies
597may be 'downgraded' by :mod:`cookielib` from version 1 to version 0 (Netscape)
598cookies.
599
600Assignment to these attributes should not be necessary other than in rare
601circumstances in a :class:`CookiePolicy` method. The class does not enforce
602internal consistency, so you should know what you're doing if you do that.
603
604
605.. attribute:: Cookie.version
606
607 Integer or :const:`None`. Netscape cookies have :attr:`version` 0. RFC 2965 and
608 RFC 2109 cookies have a ``version`` cookie-attribute of 1. However, note that
609 :mod:`cookielib` may 'downgrade' RFC 2109 cookies to Netscape cookies, in which
610 case :attr:`version` is 0.
611
612
613.. attribute:: Cookie.name
614
615 Cookie name (a string).
616
617
618.. attribute:: Cookie.value
619
620 Cookie value (a string), or :const:`None`.
621
622
623.. attribute:: Cookie.port
624
625 String representing a port or a set of ports (eg. '80', or '80,8080'), or
626 :const:`None`.
627
628
629.. attribute:: Cookie.path
630
631 Cookie path (a string, eg. ``'/acme/rocket_launchers'``).
632
633
634.. attribute:: Cookie.secure
635
636 True if cookie should only be returned over a secure connection.
637
638
639.. attribute:: Cookie.expires
640
641 Integer expiry date in seconds since epoch, or :const:`None`. See also the
642 :meth:`is_expired` method.
643
644
645.. attribute:: Cookie.discard
646
647 True if this is a session cookie.
648
649
650.. attribute:: Cookie.comment
651
652 String comment from the server explaining the function of this cookie, or
653 :const:`None`.
654
655
656.. attribute:: Cookie.comment_url
657
658 URL linking to a comment from the server explaining the function of this cookie,
659 or :const:`None`.
660
661
662.. attribute:: Cookie.rfc2109
663
664 True if this cookie was received as an RFC 2109 cookie (ie. the cookie
665 arrived in a :mailheader:`Set-Cookie` header, and the value of the Version
666 cookie-attribute in that header was 1). This attribute is provided because
667 :mod:`cookielib` may 'downgrade' RFC 2109 cookies to Netscape cookies, in
668 which case :attr:`version` is 0.
669
Georg Brandl116aa622007-08-15 14:28:22 +0000670
671.. attribute:: Cookie.port_specified
672
673 True if a port or set of ports was explicitly specified by the server (in the
674 :mailheader:`Set-Cookie` / :mailheader:`Set-Cookie2` header).
675
676
677.. attribute:: Cookie.domain_specified
678
679 True if a domain was explicitly specified by the server.
680
681
682.. attribute:: Cookie.domain_initial_dot
683
684 True if the domain explicitly specified by the server began with a dot
685 (``'.'``).
686
687Cookies may have additional non-standard cookie-attributes. These may be
688accessed using the following methods:
689
690
691.. method:: Cookie.has_nonstandard_attr(name)
692
693 Return true if cookie has the named cookie-attribute.
694
695
696.. method:: Cookie.get_nonstandard_attr(name, default=None)
697
698 If cookie has the named cookie-attribute, return its value. Otherwise, return
699 *default*.
700
701
702.. method:: Cookie.set_nonstandard_attr(name, value)
703
704 Set the value of the named cookie-attribute.
705
706The :class:`Cookie` class also defines the following method:
707
708
709.. method:: Cookie.is_expired([now=:const:`None`])
710
711 True if cookie has passed the time at which the server requested it should
712 expire. If *now* is given (in seconds since the epoch), return whether the
713 cookie has expired at the specified time.
714
715
716.. _cookielib-examples:
717
718Examples
719--------
720
721The first example shows the most common usage of :mod:`cookielib`::
722
723 import cookielib, urllib2
724 cj = cookielib.CookieJar()
725 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
726 r = opener.open("http://example.com/")
727
728This example illustrates how to open a URL using your Netscape, Mozilla, or Lynx
729cookies (assumes Unix/Netscape convention for location of the cookies file)::
730
731 import os, cookielib, urllib2
732 cj = cookielib.MozillaCookieJar()
733 cj.load(os.path.join(os.environ["HOME"], ".netscape/cookies.txt"))
734 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
735 r = opener.open("http://example.com/")
736
737The next example illustrates the use of :class:`DefaultCookiePolicy`. Turn on
738RFC 2965 cookies, be more strict about domains when setting and returning
739Netscape cookies, and block some domains from setting cookies or having them
740returned::
741
742 import urllib2
743 from cookielib import CookieJar, DefaultCookiePolicy
744 policy = DefaultCookiePolicy(
745 rfc2965=True, strict_ns_domain=Policy.DomainStrict,
746 blocked_domains=["ads.net", ".ads.net"])
747 cj = CookieJar(policy)
748 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
749 r = opener.open("http://example.com/")
750