blob: 533e963c7c50b4156e9a350ddf28ba10b7016e73 [file] [log] [blame]
Georg Brandl24420152008-05-26 16:32:26 +00001:mod:`http.cookies` --- HTTP state management
2=============================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
Georg Brandl24420152008-05-26 16:32:26 +00004.. module:: http.cookies
Georg Brandl116aa622007-08-15 14:28:22 +00005 :synopsis: Support for HTTP state management (cookies).
6.. moduleauthor:: Timothy O'Malley <timo@alum.mit.edu>
7.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
8
9
Georg Brandl24420152008-05-26 16:32:26 +000010The :mod:`http.cookies` module defines classes for abstracting the concept of
Georg Brandl116aa622007-08-15 14:28:22 +000011cookies, an HTTP state management mechanism. It supports both simple string-only
12cookies, and provides an abstraction for having any serializable data-type as
13cookie value.
14
15The module formerly strictly applied the parsing rules described in the
16:rfc:`2109` and :rfc:`2068` specifications. It has since been discovered that
17MSIE 3.0x doesn't follow the character rules outlined in those specs. As a
18result, the parsing rules used are a bit less strict.
19
20
21.. exception:: CookieError
22
23 Exception failing because of :rfc:`2109` invalidity: incorrect attributes,
24 incorrect :mailheader:`Set-Cookie` header, etc.
25
26
27.. class:: BaseCookie([input])
28
29 This class is a dictionary-like object whose keys are strings and whose values
30 are :class:`Morsel` instances. Note that upon setting a key to a value, the
31 value is first converted to a :class:`Morsel` containing the key and the value.
32
33 If *input* is given, it is passed to the :meth:`load` method.
34
35
36.. class:: SimpleCookie([input])
37
38 This class derives from :class:`BaseCookie` and overrides :meth:`value_decode`
39 and :meth:`value_encode` to be the identity and :func:`str` respectively.
40
41
Georg Brandl116aa622007-08-15 14:28:22 +000042.. seealso::
43
Georg Brandl24420152008-05-26 16:32:26 +000044 Module :mod:`http.cookiejar`
45 HTTP cookie handling for web *clients*. The :mod:`http.cookiejar` and
46 :mod:`http.cookies` modules do not depend on each other.
Georg Brandl116aa622007-08-15 14:28:22 +000047
48 :rfc:`2109` - HTTP State Management Mechanism
49 This is the state management specification implemented by this module.
50
51
52.. _cookie-objects:
53
54Cookie Objects
55--------------
56
57
58.. method:: BaseCookie.value_decode(val)
59
60 Return a decoded value from a string representation. Return value can be any
61 type. This method does nothing in :class:`BaseCookie` --- it exists so it can be
62 overridden.
63
64
65.. method:: BaseCookie.value_encode(val)
66
67 Return an encoded value. *val* can be any type, but return value must be a
68 string. This method does nothing in :class:`BaseCookie` --- it exists so it can
69 be overridden
70
71 In general, it should be the case that :meth:`value_encode` and
72 :meth:`value_decode` are inverses on the range of *value_decode*.
73
74
75.. method:: BaseCookie.output([attrs[, header[, sep]]])
76
77 Return a string representation suitable to be sent as HTTP headers. *attrs* and
78 *header* are sent to each :class:`Morsel`'s :meth:`output` method. *sep* is used
79 to join the headers together, and is by default the combination ``'\r\n'``
80 (CRLF).
81
Georg Brandl116aa622007-08-15 14:28:22 +000082
83.. method:: BaseCookie.js_output([attrs])
84
85 Return an embeddable JavaScript snippet, which, if run on a browser which
86 supports JavaScript, will act the same as if the HTTP headers was sent.
87
88 The meaning for *attrs* is the same as in :meth:`output`.
89
90
91.. method:: BaseCookie.load(rawdata)
92
93 If *rawdata* is a string, parse it as an ``HTTP_COOKIE`` and add the values
94 found there as :class:`Morsel`\ s. If it is a dictionary, it is equivalent to::
95
96 for k, v in rawdata.items():
97 cookie[k] = v
98
99
100.. _morsel-objects:
101
102Morsel Objects
103--------------
104
105
106.. class:: Morsel()
107
108 Abstract a key/value pair, which has some :rfc:`2109` attributes.
109
110 Morsels are dictionary-like objects, whose set of keys is constant --- the valid
111 :rfc:`2109` attributes, which are
112
113 * ``expires``
114 * ``path``
115 * ``comment``
116 * ``domain``
117 * ``max-age``
118 * ``secure``
119 * ``version``
120
121 The keys are case-insensitive.
122
123
124.. attribute:: Morsel.value
125
126 The value of the cookie.
127
128
129.. attribute:: Morsel.coded_value
130
131 The encoded value of the cookie --- this is what should be sent.
132
133
134.. attribute:: Morsel.key
135
136 The name of the cookie.
137
138
139.. method:: Morsel.set(key, value, coded_value)
140
141 Set the *key*, *value* and *coded_value* members.
142
143
144.. method:: Morsel.isReservedKey(K)
145
146 Whether *K* is a member of the set of keys of a :class:`Morsel`.
147
148
149.. method:: Morsel.output([attrs[, header]])
150
151 Return a string representation of the Morsel, suitable to be sent as an HTTP
152 header. By default, all the attributes are included, unless *attrs* is given, in
153 which case it should be a list of attributes to use. *header* is by default
154 ``"Set-Cookie:"``.
155
156
157.. method:: Morsel.js_output([attrs])
158
159 Return an embeddable JavaScript snippet, which, if run on a browser which
160 supports JavaScript, will act the same as if the HTTP header was sent.
161
162 The meaning for *attrs* is the same as in :meth:`output`.
163
164
165.. method:: Morsel.OutputString([attrs])
166
167 Return a string representing the Morsel, without any surrounding HTTP or
168 JavaScript.
169
170 The meaning for *attrs* is the same as in :meth:`output`.
171
172
173.. _cookie-example:
174
175Example
176-------
177
Georg Brandl24420152008-05-26 16:32:26 +0000178The following example demonstrates how to use the :mod:`http.cookies` module.
Christian Heimesfe337bf2008-03-23 21:54:12 +0000179
180.. doctest::
181 :options: +NORMALIZE_WHITESPACE
Georg Brandl116aa622007-08-15 14:28:22 +0000182
Georg Brandl24420152008-05-26 16:32:26 +0000183 >>> from http import cookies
184 >>> C = cookies.SimpleCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000185 >>> C["fig"] = "newton"
186 >>> C["sugar"] = "wafer"
Georg Brandl6911e3c2007-09-04 07:15:32 +0000187 >>> print(C) # generate HTTP headers
Georg Brandl116aa622007-08-15 14:28:22 +0000188 Set-Cookie: fig=newton
Christian Heimesfe337bf2008-03-23 21:54:12 +0000189 Set-Cookie: sugar=wafer
Georg Brandl6911e3c2007-09-04 07:15:32 +0000190 >>> print(C.output()) # same thing
Georg Brandl116aa622007-08-15 14:28:22 +0000191 Set-Cookie: fig=newton
Christian Heimesfe337bf2008-03-23 21:54:12 +0000192 Set-Cookie: sugar=wafer
Georg Brandl61013952008-05-28 15:56:30 +0000193 >>> C = cookies.SimpleCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000194 >>> C["rocky"] = "road"
195 >>> C["rocky"]["path"] = "/cookie"
Georg Brandl6911e3c2007-09-04 07:15:32 +0000196 >>> print(C.output(header="Cookie:"))
Georg Brandl116aa622007-08-15 14:28:22 +0000197 Cookie: rocky=road; Path=/cookie
Georg Brandl6911e3c2007-09-04 07:15:32 +0000198 >>> print(C.output(attrs=[], header="Cookie:"))
Georg Brandl116aa622007-08-15 14:28:22 +0000199 Cookie: rocky=road
Georg Brandl61013952008-05-28 15:56:30 +0000200 >>> C = cookies.SimpleCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000201 >>> C.load("chips=ahoy; vienna=finger") # load from a string (HTTP header)
Georg Brandl6911e3c2007-09-04 07:15:32 +0000202 >>> print(C)
Georg Brandl116aa622007-08-15 14:28:22 +0000203 Set-Cookie: chips=ahoy
Christian Heimesfe337bf2008-03-23 21:54:12 +0000204 Set-Cookie: vienna=finger
Georg Brandl61013952008-05-28 15:56:30 +0000205 >>> C = cookies.SimpleCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000206 >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
Georg Brandl6911e3c2007-09-04 07:15:32 +0000207 >>> print(C)
Georg Brandl116aa622007-08-15 14:28:22 +0000208 Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
Georg Brandl61013952008-05-28 15:56:30 +0000209 >>> C = cookies.SimpleCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000210 >>> C["oreo"] = "doublestuff"
211 >>> C["oreo"]["path"] = "/"
Georg Brandl6911e3c2007-09-04 07:15:32 +0000212 >>> print(C)
Georg Brandl116aa622007-08-15 14:28:22 +0000213 Set-Cookie: oreo=doublestuff; Path=/
Georg Brandl61013952008-05-28 15:56:30 +0000214 >>> C = cookies.SimpleCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000215 >>> C["twix"] = "none for you"
216 >>> C["twix"].value
217 'none for you'
Georg Brandl24420152008-05-26 16:32:26 +0000218 >>> C = cookies.SimpleCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000219 >>> C["number"] = 7 # equivalent to C["number"] = str(7)
220 >>> C["string"] = "seven"
221 >>> C["number"].value
222 '7'
223 >>> C["string"].value
224 'seven'
Georg Brandl6911e3c2007-09-04 07:15:32 +0000225 >>> print(C)
Georg Brandl116aa622007-08-15 14:28:22 +0000226 Set-Cookie: number=7
227 Set-Cookie: string=seven