1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>21.23. http.cookies — HTTP state management — Python 3.5.1 documentation</title>
11
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 3.5.1 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 3.5.1 documentation" href="../contents.html" />
33
<link rel="up" title="21. Internet Protocols and Support" href="internet.html" />
34
<link rel="next" title="21.24. http.cookiejar — Cookie handling for HTTP clients" href="http.cookiejar.html" />
35
<link rel="prev" title="21.22. http.server — HTTP servers" href="http.server.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="http.cookiejar.html" title="21.24. http.cookiejar — Cookie handling for HTTP clients"
55
accesskey="N">next</a> |</li>
57
<a href="http.server.html" title="21.22. http.server — HTTP servers"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">3.5.1</span>
64
<a href="../index.html">Documentation </a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">21. Internet Protocols and Support</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-http.cookies">
78
<span id="http-cookies-http-state-management"></span><h1>21.23. <a class="reference internal" href="#module-http.cookies" title="http.cookies: Support for HTTP state management (cookies)."><code class="xref py py-mod docutils literal"><span class="pre">http.cookies</span></code></a> — HTTP state management<a class="headerlink" href="#module-http.cookies" title="Permalink to this headline">¶</a></h1>
79
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/http/cookies.py">Lib/http/cookies.py</a></p>
80
<hr class="docutils" />
81
<p>The <a class="reference internal" href="#module-http.cookies" title="http.cookies: Support for HTTP state management (cookies)."><code class="xref py py-mod docutils literal"><span class="pre">http.cookies</span></code></a> module defines classes for abstracting the concept of
82
cookies, an HTTP state management mechanism. It supports both simple string-only
83
cookies, and provides an abstraction for having any serializable data-type as
85
<p>The module formerly strictly applied the parsing rules described in the
86
<span class="target" id="index-0"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2109.html"><strong>RFC 2109</strong></a> and <span class="target" id="index-1"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2068.html"><strong>RFC 2068</strong></a> specifications. It has since been discovered that
87
MSIE 3.0x doesn’t follow the character rules outlined in those specs and also
88
many current day browsers and servers have relaxed parsing rules when comes to
89
Cookie handling. As a result, the parsing rules used are a bit less strict.</p>
90
<p>The character set, <a class="reference internal" href="string.html#string.ascii_letters" title="string.ascii_letters"><code class="xref py py-data docutils literal"><span class="pre">string.ascii_letters</span></code></a>, <a class="reference internal" href="string.html#string.digits" title="string.digits"><code class="xref py py-data docutils literal"><span class="pre">string.digits</span></code></a> and
91
<code class="docutils literal"><span class="pre">!#$%&'*+-.^_`|~:</span></code> denote the set of valid characters allowed by this module
92
in Cookie name (as <a class="reference internal" href="#http.cookies.Morsel.key" title="http.cookies.Morsel.key"><code class="xref py py-attr docutils literal"><span class="pre">key</span></code></a>).</p>
93
<div class="versionchanged">
94
<p><span class="versionmodified">Changed in version 3.3: </span>Allowed ‘:’ as a valid Cookie name character.</p>
96
<div class="admonition note">
97
<p class="first admonition-title">Note</p>
98
<p class="last">On encountering an invalid cookie, <a class="reference internal" href="#http.cookies.CookieError" title="http.cookies.CookieError"><code class="xref py py-exc docutils literal"><span class="pre">CookieError</span></code></a> is raised, so if your
99
cookie data comes from a browser you should always prepare for invalid data
100
and catch <a class="reference internal" href="#http.cookies.CookieError" title="http.cookies.CookieError"><code class="xref py py-exc docutils literal"><span class="pre">CookieError</span></code></a> on parsing.</p>
102
<dl class="exception">
103
<dt id="http.cookies.CookieError">
104
<em class="property">exception </em><code class="descclassname">http.cookies.</code><code class="descname">CookieError</code><a class="headerlink" href="#http.cookies.CookieError" title="Permalink to this definition">¶</a></dt>
105
<dd><p>Exception failing because of <span class="target" id="index-2"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2109.html"><strong>RFC 2109</strong></a> invalidity: incorrect attributes,
106
incorrect <em class="mailheader">Set-Cookie</em> header, etc.</p>
110
<dt id="http.cookies.BaseCookie">
111
<em class="property">class </em><code class="descclassname">http.cookies.</code><code class="descname">BaseCookie</code><span class="sig-paren">(</span><span class="optional">[</span><em>input</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.BaseCookie" title="Permalink to this definition">¶</a></dt>
112
<dd><p>This class is a dictionary-like object whose keys are strings and whose values
113
are <a class="reference internal" href="#http.cookies.Morsel" title="http.cookies.Morsel"><code class="xref py py-class docutils literal"><span class="pre">Morsel</span></code></a> instances. Note that upon setting a key to a value, the
114
value is first converted to a <a class="reference internal" href="#http.cookies.Morsel" title="http.cookies.Morsel"><code class="xref py py-class docutils literal"><span class="pre">Morsel</span></code></a> containing the key and the value.</p>
115
<p>If <em>input</em> is given, it is passed to the <a class="reference internal" href="#http.cookies.BaseCookie.load" title="http.cookies.BaseCookie.load"><code class="xref py py-meth docutils literal"><span class="pre">load()</span></code></a> method.</p>
119
<dt id="http.cookies.SimpleCookie">
120
<em class="property">class </em><code class="descclassname">http.cookies.</code><code class="descname">SimpleCookie</code><span class="sig-paren">(</span><span class="optional">[</span><em>input</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.SimpleCookie" title="Permalink to this definition">¶</a></dt>
121
<dd><p>This class derives from <a class="reference internal" href="#http.cookies.BaseCookie" title="http.cookies.BaseCookie"><code class="xref py py-class docutils literal"><span class="pre">BaseCookie</span></code></a> and overrides <code class="xref py py-meth docutils literal"><span class="pre">value_decode()</span></code>
122
and <code class="xref py py-meth docutils literal"><span class="pre">value_encode()</span></code> to be the identity and <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal"><span class="pre">str()</span></code></a> respectively.</p>
125
<div class="admonition seealso">
126
<p class="first admonition-title">See also</p>
127
<dl class="last docutils">
128
<dt>Module <a class="reference internal" href="http.cookiejar.html#module-http.cookiejar" title="http.cookiejar: Classes for automatic handling of HTTP cookies."><code class="xref py py-mod docutils literal"><span class="pre">http.cookiejar</span></code></a></dt>
129
<dd>HTTP cookie handling for web <em>clients</em>. The <a class="reference internal" href="http.cookiejar.html#module-http.cookiejar" title="http.cookiejar: Classes for automatic handling of HTTP cookies."><code class="xref py py-mod docutils literal"><span class="pre">http.cookiejar</span></code></a> and
130
<a class="reference internal" href="#module-http.cookies" title="http.cookies: Support for HTTP state management (cookies)."><code class="xref py py-mod docutils literal"><span class="pre">http.cookies</span></code></a> modules do not depend on each other.</dd>
131
<dt><span class="target" id="index-3"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2109.html"><strong>RFC 2109</strong></a> - HTTP State Management Mechanism</dt>
132
<dd>This is the state management specification implemented by this module.</dd>
135
<div class="section" id="cookie-objects">
136
<span id="id1"></span><h2>21.23.1. Cookie Objects<a class="headerlink" href="#cookie-objects" title="Permalink to this headline">¶</a></h2>
138
<dt id="http.cookies.BaseCookie.value_decode">
139
<code class="descclassname">BaseCookie.</code><code class="descname">value_decode</code><span class="sig-paren">(</span><em>val</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.BaseCookie.value_decode" title="Permalink to this definition">¶</a></dt>
140
<dd><p>Return a decoded value from a string representation. Return value can be any
141
type. This method does nothing in <a class="reference internal" href="#http.cookies.BaseCookie" title="http.cookies.BaseCookie"><code class="xref py py-class docutils literal"><span class="pre">BaseCookie</span></code></a> — it exists so it can be
146
<dt id="http.cookies.BaseCookie.value_encode">
147
<code class="descclassname">BaseCookie.</code><code class="descname">value_encode</code><span class="sig-paren">(</span><em>val</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.BaseCookie.value_encode" title="Permalink to this definition">¶</a></dt>
148
<dd><p>Return an encoded value. <em>val</em> can be any type, but return value must be a
149
string. This method does nothing in <a class="reference internal" href="#http.cookies.BaseCookie" title="http.cookies.BaseCookie"><code class="xref py py-class docutils literal"><span class="pre">BaseCookie</span></code></a> — it exists so it can
151
<p>In general, it should be the case that <a class="reference internal" href="#http.cookies.BaseCookie.value_encode" title="http.cookies.BaseCookie.value_encode"><code class="xref py py-meth docutils literal"><span class="pre">value_encode()</span></code></a> and
152
<a class="reference internal" href="#http.cookies.BaseCookie.value_decode" title="http.cookies.BaseCookie.value_decode"><code class="xref py py-meth docutils literal"><span class="pre">value_decode()</span></code></a> are inverses on the range of <em>value_decode</em>.</p>
156
<dt id="http.cookies.BaseCookie.output">
157
<code class="descclassname">BaseCookie.</code><code class="descname">output</code><span class="sig-paren">(</span><em>attrs=None</em>, <em>header='Set-Cookie:'</em>, <em>sep='\r\n'</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.BaseCookie.output" title="Permalink to this definition">¶</a></dt>
158
<dd><p>Return a string representation suitable to be sent as HTTP headers. <em>attrs</em> and
159
<em>header</em> are sent to each <a class="reference internal" href="#http.cookies.Morsel" title="http.cookies.Morsel"><code class="xref py py-class docutils literal"><span class="pre">Morsel</span></code></a>‘s <a class="reference internal" href="#http.cookies.BaseCookie.output" title="http.cookies.BaseCookie.output"><code class="xref py py-meth docutils literal"><span class="pre">output()</span></code></a> method. <em>sep</em> is used
160
to join the headers together, and is by default the combination <code class="docutils literal"><span class="pre">'\r\n'</span></code>
165
<dt id="http.cookies.BaseCookie.js_output">
166
<code class="descclassname">BaseCookie.</code><code class="descname">js_output</code><span class="sig-paren">(</span><em>attrs=None</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.BaseCookie.js_output" title="Permalink to this definition">¶</a></dt>
167
<dd><p>Return an embeddable JavaScript snippet, which, if run on a browser which
168
supports JavaScript, will act the same as if the HTTP headers was sent.</p>
169
<p>The meaning for <em>attrs</em> is the same as in <a class="reference internal" href="#http.cookies.BaseCookie.output" title="http.cookies.BaseCookie.output"><code class="xref py py-meth docutils literal"><span class="pre">output()</span></code></a>.</p>
173
<dt id="http.cookies.BaseCookie.load">
174
<code class="descclassname">BaseCookie.</code><code class="descname">load</code><span class="sig-paren">(</span><em>rawdata</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.BaseCookie.load" title="Permalink to this definition">¶</a></dt>
175
<dd><p>If <em>rawdata</em> is a string, parse it as an <code class="docutils literal"><span class="pre">HTTP_COOKIE</span></code> and add the values
176
found there as <a class="reference internal" href="#http.cookies.Morsel" title="http.cookies.Morsel"><code class="xref py py-class docutils literal"><span class="pre">Morsel</span></code></a>s. If it is a dictionary, it is equivalent to:</p>
177
<div class="highlight-python3"><div class="highlight"><pre><span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">rawdata</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
178
<span class="n">cookie</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>
184
<div class="section" id="morsel-objects">
185
<span id="id2"></span><h2>21.23.2. Morsel Objects<a class="headerlink" href="#morsel-objects" title="Permalink to this headline">¶</a></h2>
187
<dt id="http.cookies.Morsel">
188
<em class="property">class </em><code class="descclassname">http.cookies.</code><code class="descname">Morsel</code><a class="headerlink" href="#http.cookies.Morsel" title="Permalink to this definition">¶</a></dt>
189
<dd><p>Abstract a key/value pair, which has some <span class="target" id="index-4"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2109.html"><strong>RFC 2109</strong></a> attributes.</p>
190
<p>Morsels are dictionary-like objects, whose set of keys is constant — the valid
191
<span class="target" id="index-5"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2109.html"><strong>RFC 2109</strong></a> attributes, which are</p>
193
<li><code class="docutils literal"><span class="pre">expires</span></code></li>
194
<li><code class="docutils literal"><span class="pre">path</span></code></li>
195
<li><code class="docutils literal"><span class="pre">comment</span></code></li>
196
<li><code class="docutils literal"><span class="pre">domain</span></code></li>
197
<li><code class="docutils literal"><span class="pre">max-age</span></code></li>
198
<li><code class="docutils literal"><span class="pre">secure</span></code></li>
199
<li><code class="docutils literal"><span class="pre">version</span></code></li>
200
<li><code class="docutils literal"><span class="pre">httponly</span></code></li>
202
<p>The attribute <code class="xref py py-attr docutils literal"><span class="pre">httponly</span></code> specifies that the cookie is only transferred
203
in HTTP requests, and is not accessible through JavaScript. This is intended
204
to mitigate some forms of cross-site scripting.</p>
205
<p>The keys are case-insensitive.</p>
206
<div class="versionchanged">
207
<p><span class="versionmodified">Changed in version 3.5: </span><code class="xref py py-meth docutils literal"><span class="pre">__eq__()</span></code> now takes <a class="reference internal" href="#http.cookies.Morsel.key" title="http.cookies.Morsel.key"><code class="xref py py-attr docutils literal"><span class="pre">key</span></code></a> and <a class="reference internal" href="#http.cookies.Morsel.value" title="http.cookies.Morsel.value"><code class="xref py py-attr docutils literal"><span class="pre">value</span></code></a>
212
<dl class="attribute">
213
<dt id="http.cookies.Morsel.value">
214
<code class="descclassname">Morsel.</code><code class="descname">value</code><a class="headerlink" href="#http.cookies.Morsel.value" title="Permalink to this definition">¶</a></dt>
215
<dd><p>The value of the cookie.</p>
216
<div class="deprecated">
217
<p><span class="versionmodified">Deprecated since version 3.5: </span>assigning to <code class="docutils literal"><span class="pre">value</span></code>; use <a class="reference internal" href="#http.cookies.Morsel.set" title="http.cookies.Morsel.set"><code class="xref py py-meth docutils literal"><span class="pre">set()</span></code></a> instead.</p>
221
<dl class="attribute">
222
<dt id="http.cookies.Morsel.coded_value">
223
<code class="descclassname">Morsel.</code><code class="descname">coded_value</code><a class="headerlink" href="#http.cookies.Morsel.coded_value" title="Permalink to this definition">¶</a></dt>
224
<dd><p>The encoded value of the cookie — this is what should be sent.</p>
225
<div class="deprecated">
226
<p><span class="versionmodified">Deprecated since version 3.5: </span>assigning to <code class="docutils literal"><span class="pre">coded_value</span></code>; use <a class="reference internal" href="#http.cookies.Morsel.set" title="http.cookies.Morsel.set"><code class="xref py py-meth docutils literal"><span class="pre">set()</span></code></a> instead.</p>
230
<dl class="attribute">
231
<dt id="http.cookies.Morsel.key">
232
<code class="descclassname">Morsel.</code><code class="descname">key</code><a class="headerlink" href="#http.cookies.Morsel.key" title="Permalink to this definition">¶</a></dt>
233
<dd><p>The name of the cookie.</p>
234
<div class="deprecated">
235
<p><span class="versionmodified">Deprecated since version 3.5: </span>assigning to <code class="docutils literal"><span class="pre">key</span></code>; use <a class="reference internal" href="#http.cookies.Morsel.set" title="http.cookies.Morsel.set"><code class="xref py py-meth docutils literal"><span class="pre">set()</span></code></a> instead.</p>
240
<dt id="http.cookies.Morsel.set">
241
<code class="descclassname">Morsel.</code><code class="descname">set</code><span class="sig-paren">(</span><em>key</em>, <em>value</em>, <em>coded_value</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.Morsel.set" title="Permalink to this definition">¶</a></dt>
242
<dd><p>Set the <em>key</em>, <em>value</em> and <em>coded_value</em> attributes.</p>
243
<div class="deprecated">
244
<p><span class="versionmodified">Deprecated since version 3.5: </span>The undocumented <em>LegalChars</em> parameter is ignored and will be removed in
245
a future version.</p>
250
<dt id="http.cookies.Morsel.isReservedKey">
251
<code class="descclassname">Morsel.</code><code class="descname">isReservedKey</code><span class="sig-paren">(</span><em>K</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.Morsel.isReservedKey" title="Permalink to this definition">¶</a></dt>
252
<dd><p>Whether <em>K</em> is a member of the set of keys of a <a class="reference internal" href="#http.cookies.Morsel" title="http.cookies.Morsel"><code class="xref py py-class docutils literal"><span class="pre">Morsel</span></code></a>.</p>
256
<dt id="http.cookies.Morsel.output">
257
<code class="descclassname">Morsel.</code><code class="descname">output</code><span class="sig-paren">(</span><em>attrs=None</em>, <em>header='Set-Cookie:'</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.Morsel.output" title="Permalink to this definition">¶</a></dt>
258
<dd><p>Return a string representation of the Morsel, suitable to be sent as an HTTP
259
header. By default, all the attributes are included, unless <em>attrs</em> is given, in
260
which case it should be a list of attributes to use. <em>header</em> is by default
261
<code class="docutils literal"><span class="pre">"Set-Cookie:"</span></code>.</p>
265
<dt id="http.cookies.Morsel.js_output">
266
<code class="descclassname">Morsel.</code><code class="descname">js_output</code><span class="sig-paren">(</span><em>attrs=None</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.Morsel.js_output" title="Permalink to this definition">¶</a></dt>
267
<dd><p>Return an embeddable JavaScript snippet, which, if run on a browser which
268
supports JavaScript, will act the same as if the HTTP header was sent.</p>
269
<p>The meaning for <em>attrs</em> is the same as in <a class="reference internal" href="#http.cookies.Morsel.output" title="http.cookies.Morsel.output"><code class="xref py py-meth docutils literal"><span class="pre">output()</span></code></a>.</p>
273
<dt id="http.cookies.Morsel.OutputString">
274
<code class="descclassname">Morsel.</code><code class="descname">OutputString</code><span class="sig-paren">(</span><em>attrs=None</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.Morsel.OutputString" title="Permalink to this definition">¶</a></dt>
275
<dd><p>Return a string representing the Morsel, without any surrounding HTTP or
277
<p>The meaning for <em>attrs</em> is the same as in <a class="reference internal" href="#http.cookies.Morsel.output" title="http.cookies.Morsel.output"><code class="xref py py-meth docutils literal"><span class="pre">output()</span></code></a>.</p>
281
<dt id="http.cookies.Morsel.update">
282
<code class="descclassname">Morsel.</code><code class="descname">update</code><span class="sig-paren">(</span><em>values</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.Morsel.update" title="Permalink to this definition">¶</a></dt>
283
<dd><p>Update the values in the Morsel dictionary with the values in the dictionary
284
<em>values</em>. Raise an error if any of the keys in the <em>values</em> dict is not a
285
valid <span class="target" id="index-6"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2109.html"><strong>RFC 2109</strong></a> attribute.</p>
286
<div class="versionchanged">
287
<p><span class="versionmodified">Changed in version 3.5: </span>an error is raised for invalid keys.</p>
292
<dt id="http.cookies.Morsel.copy">
293
<code class="descclassname">Morsel.</code><code class="descname">copy</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.Morsel.copy" title="Permalink to this definition">¶</a></dt>
294
<dd><p>Return a shallow copy of the Morsel object.</p>
295
<div class="versionchanged">
296
<p><span class="versionmodified">Changed in version 3.5: </span>return a Morsel object instead of a dict.</p>
301
<dt id="http.cookies.Morsel.setdefault">
302
<code class="descclassname">Morsel.</code><code class="descname">setdefault</code><span class="sig-paren">(</span><em>key</em>, <em>value=None</em><span class="sig-paren">)</span><a class="headerlink" href="#http.cookies.Morsel.setdefault" title="Permalink to this definition">¶</a></dt>
303
<dd><p>Raise an error if key is not a valid <span class="target" id="index-7"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2109.html"><strong>RFC 2109</strong></a> attribute, otherwise
304
behave the same as <a class="reference internal" href="stdtypes.html#dict.setdefault" title="dict.setdefault"><code class="xref py py-meth docutils literal"><span class="pre">dict.setdefault()</span></code></a>.</p>
308
<div class="section" id="example">
309
<span id="cookie-example"></span><h2>21.23.3. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
310
<p>The following example demonstrates how to use the <a class="reference internal" href="#module-http.cookies" title="http.cookies: Support for HTTP state management (cookies)."><code class="xref py py-mod docutils literal"><span class="pre">http.cookies</span></code></a> module.</p>
311
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">http</span> <span class="k">import</span> <span class="n">cookies</span>
312
<span class="gp">>>> </span><span class="n">C</span> <span class="o">=</span> <span class="n">cookies</span><span class="o">.</span><span class="n">SimpleCookie</span><span class="p">()</span>
313
<span class="gp">>>> </span><span class="n">C</span><span class="p">[</span><span class="s2">"fig"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"newton"</span>
314
<span class="gp">>>> </span><span class="n">C</span><span class="p">[</span><span class="s2">"sugar"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"wafer"</span>
315
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">C</span><span class="p">)</span> <span class="c1"># generate HTTP headers</span>
316
<span class="go">Set-Cookie: fig=newton</span>
317
<span class="go">Set-Cookie: sugar=wafer</span>
318
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">C</span><span class="o">.</span><span class="n">output</span><span class="p">())</span> <span class="c1"># same thing</span>
319
<span class="go">Set-Cookie: fig=newton</span>
320
<span class="go">Set-Cookie: sugar=wafer</span>
321
<span class="gp">>>> </span><span class="n">C</span> <span class="o">=</span> <span class="n">cookies</span><span class="o">.</span><span class="n">SimpleCookie</span><span class="p">()</span>
322
<span class="gp">>>> </span><span class="n">C</span><span class="p">[</span><span class="s2">"rocky"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"road"</span>
323
<span class="gp">>>> </span><span class="n">C</span><span class="p">[</span><span class="s2">"rocky"</span><span class="p">][</span><span class="s2">"path"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"/cookie"</span>
324
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">C</span><span class="o">.</span><span class="n">output</span><span class="p">(</span><span class="n">header</span><span class="o">=</span><span class="s2">"Cookie:"</span><span class="p">))</span>
325
<span class="go">Cookie: rocky=road; Path=/cookie</span>
326
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">C</span><span class="o">.</span><span class="n">output</span><span class="p">(</span><span class="n">attrs</span><span class="o">=</span><span class="p">[],</span> <span class="n">header</span><span class="o">=</span><span class="s2">"Cookie:"</span><span class="p">))</span>
327
<span class="go">Cookie: rocky=road</span>
328
<span class="gp">>>> </span><span class="n">C</span> <span class="o">=</span> <span class="n">cookies</span><span class="o">.</span><span class="n">SimpleCookie</span><span class="p">()</span>
329
<span class="gp">>>> </span><span class="n">C</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s2">"chips=ahoy; vienna=finger"</span><span class="p">)</span> <span class="c1"># load from a string (HTTP header)</span>
330
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">C</span><span class="p">)</span>
331
<span class="go">Set-Cookie: chips=ahoy</span>
332
<span class="go">Set-Cookie: vienna=finger</span>
333
<span class="gp">>>> </span><span class="n">C</span> <span class="o">=</span> <span class="n">cookies</span><span class="o">.</span><span class="n">SimpleCookie</span><span class="p">()</span>
334
<span class="gp">>>> </span><span class="n">C</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s1">'keebler="E=everybody; L=</span><span class="se">\\</span><span class="s1">"Loves</span><span class="se">\\</span><span class="s1">"; fudge=</span><span class="se">\\</span><span class="s1">012;";'</span><span class="p">)</span>
335
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">C</span><span class="p">)</span>
336
<span class="go">Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"</span>
337
<span class="gp">>>> </span><span class="n">C</span> <span class="o">=</span> <span class="n">cookies</span><span class="o">.</span><span class="n">SimpleCookie</span><span class="p">()</span>
338
<span class="gp">>>> </span><span class="n">C</span><span class="p">[</span><span class="s2">"oreo"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"doublestuff"</span>
339
<span class="gp">>>> </span><span class="n">C</span><span class="p">[</span><span class="s2">"oreo"</span><span class="p">][</span><span class="s2">"path"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"/"</span>
340
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">C</span><span class="p">)</span>
341
<span class="go">Set-Cookie: oreo=doublestuff; Path=/</span>
342
<span class="gp">>>> </span><span class="n">C</span> <span class="o">=</span> <span class="n">cookies</span><span class="o">.</span><span class="n">SimpleCookie</span><span class="p">()</span>
343
<span class="gp">>>> </span><span class="n">C</span><span class="p">[</span><span class="s2">"twix"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"none for you"</span>
344
<span class="gp">>>> </span><span class="n">C</span><span class="p">[</span><span class="s2">"twix"</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>
345
<span class="go">'none for you'</span>
346
<span class="gp">>>> </span><span class="n">C</span> <span class="o">=</span> <span class="n">cookies</span><span class="o">.</span><span class="n">SimpleCookie</span><span class="p">()</span>
347
<span class="gp">>>> </span><span class="n">C</span><span class="p">[</span><span class="s2">"number"</span><span class="p">]</span> <span class="o">=</span> <span class="mi">7</span> <span class="c1"># equivalent to C["number"] = str(7)</span>
348
<span class="gp">>>> </span><span class="n">C</span><span class="p">[</span><span class="s2">"string"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"seven"</span>
349
<span class="gp">>>> </span><span class="n">C</span><span class="p">[</span><span class="s2">"number"</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>
350
<span class="go">'7'</span>
351
<span class="gp">>>> </span><span class="n">C</span><span class="p">[</span><span class="s2">"string"</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>
352
<span class="go">'seven'</span>
353
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">C</span><span class="p">)</span>
354
<span class="go">Set-Cookie: number=7</span>
355
<span class="go">Set-Cookie: string=seven</span>
365
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
366
<div class="sphinxsidebarwrapper">
367
<h3><a href="../contents.html">Table Of Contents</a></h3>
369
<li><a class="reference internal" href="#">21.23. <code class="docutils literal"><span class="pre">http.cookies</span></code> — HTTP state management</a><ul>
370
<li><a class="reference internal" href="#cookie-objects">21.23.1. Cookie Objects</a></li>
371
<li><a class="reference internal" href="#morsel-objects">21.23.2. Morsel Objects</a></li>
372
<li><a class="reference internal" href="#example">21.23.3. Example</a></li>
377
<h4>Previous topic</h4>
378
<p class="topless"><a href="http.server.html"
379
title="previous chapter">21.22. <code class="docutils literal"><span class="pre">http.server</span></code> — HTTP servers</a></p>
381
<p class="topless"><a href="http.cookiejar.html"
382
title="next chapter">21.24. <code class="docutils literal"><span class="pre">http.cookiejar</span></code> — Cookie handling for HTTP clients</a></p>
384
<ul class="this-page-menu">
385
<li><a href="../bugs.html">Report a Bug</a></li>
386
<li><a href="../_sources/library/http.cookies.txt"
387
rel="nofollow">Show Source</a></li>
390
<div id="searchbox" style="display: none" role="search">
391
<h3>Quick search</h3>
392
<form class="search" action="../search.html" method="get">
393
<input type="text" name="q" />
394
<input type="submit" value="Go" />
395
<input type="hidden" name="check_keywords" value="yes" />
396
<input type="hidden" name="area" value="default" />
398
<p class="searchtip" style="font-size: 90%">
399
Enter search terms or a module, class or function name.
402
<script type="text/javascript">$('#searchbox').show(0);</script>
405
<div class="clearer"></div>
407
<div class="related" role="navigation" aria-label="related navigation">
410
<li class="right" style="margin-right: 10px">
411
<a href="../genindex.html" title="General Index"
414
<a href="../py-modindex.html" title="Python Module Index"
417
<a href="http.cookiejar.html" title="21.24. http.cookiejar — Cookie handling for HTTP clients"
420
<a href="http.server.html" title="21.22. http.server — HTTP servers"
422
<li><img src="../_static/py.png" alt=""
423
style="vertical-align: middle; margin-top: -1px"/></li>
424
<li><a href="https://www.python.org/">Python</a> »</li>
426
<span class="version_switcher_placeholder">3.5.1</span>
427
<a href="../index.html">Documentation </a> »
430
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
431
<li class="nav-item nav-item-2"><a href="internet.html" >21. Internet Protocols and Support</a> »</li>
435
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
437
The Python Software Foundation is a non-profit corporation.
438
<a href="https://www.python.org/psf/donations/">Please donate.</a>
440
Last updated on Jan 22, 2016.
441
<a href="../bugs.html">Found a bug</a>?
443
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'