~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.1-docs-html/library/http.cookies.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>21.23. http.cookies — HTTP state management &mdash; Python 3.5.1 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '3.5.1',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
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>
39
 
    
40
 
 
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="http.cookiejar.html" title="21.24. http.cookiejar — Cookie handling for HTTP clients"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
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> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">3.5.1</span>
64
 
          <a href="../index.html">Documentation </a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
68
 
          <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">21. Internet Protocols and Support</a> &raquo;</li> 
69
 
      </ul>
70
 
    </div>    
71
 
 
72
 
    <div class="document">
73
 
      <div class="documentwrapper">
74
 
        <div class="bodywrapper">
75
 
          <div class="body" role="main">
76
 
            
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> &#8212; 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
84
 
cookie value.</p>
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&#8217;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">!#$%&amp;'*+-.^_`|~:</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 &#8216;:&#8217; as a valid Cookie name character.</p>
95
 
</div>
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>
101
 
</div>
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>
107
 
</dd></dl>
108
 
 
109
 
<dl class="class">
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>
116
 
</dd></dl>
117
 
 
118
 
<dl class="class">
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>
123
 
</dd></dl>
124
 
 
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>
133
 
</dl>
134
 
</div>
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>
137
 
<dl class="method">
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> &#8212; it exists so it can be
142
 
overridden.</p>
143
 
</dd></dl>
144
 
 
145
 
<dl class="method">
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> &#8212; it exists so it can
150
 
be overridden.</p>
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>
153
 
</dd></dl>
154
 
 
155
 
<dl class="method">
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>&#8216;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>
161
 
(CRLF).</p>
162
 
</dd></dl>
163
 
 
164
 
<dl class="method">
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>
170
 
</dd></dl>
171
 
 
172
 
<dl class="method">
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>
179
 
</pre></div>
180
 
</div>
181
 
</dd></dl>
182
 
 
183
 
</div>
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>
186
 
<dl class="class">
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 &#8212; 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>
192
 
<ul class="simple">
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>
201
 
</ul>
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>
208
 
into account.</p>
209
 
</div>
210
 
</dd></dl>
211
 
 
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>
218
 
</div>
219
 
</dd></dl>
220
 
 
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 &#8212; 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>
227
 
</div>
228
 
</dd></dl>
229
 
 
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>
236
 
</div>
237
 
</dd></dl>
238
 
 
239
 
<dl class="method">
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>
246
 
</div>
247
 
</dd></dl>
248
 
 
249
 
<dl class="method">
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>
253
 
</dd></dl>
254
 
 
255
 
<dl class="method">
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">&quot;Set-Cookie:&quot;</span></code>.</p>
262
 
</dd></dl>
263
 
 
264
 
<dl class="method">
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>
270
 
</dd></dl>
271
 
 
272
 
<dl class="method">
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
276
 
JavaScript.</p>
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>
278
 
</dd></dl>
279
 
 
280
 
<dl class="method">
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>
288
 
</div>
289
 
</dd></dl>
290
 
 
291
 
<dl class="method">
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>
297
 
</div>
298
 
</dd></dl>
299
 
 
300
 
<dl class="method">
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>
305
 
</dd></dl>
306
 
 
307
 
</div>
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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">[</span><span class="s2">&quot;fig&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;newton&quot;</span>
314
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">[</span><span class="s2">&quot;sugar&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;wafer&quot;</span>
315
 
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">[</span><span class="s2">&quot;rocky&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;road&quot;</span>
323
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">[</span><span class="s2">&quot;rocky&quot;</span><span class="p">][</span><span class="s2">&quot;path&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;/cookie&quot;</span>
324
 
<span class="gp">&gt;&gt;&gt; </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">&quot;Cookie:&quot;</span><span class="p">))</span>
325
 
<span class="go">Cookie: rocky=road; Path=/cookie</span>
326
 
<span class="gp">&gt;&gt;&gt; </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">&quot;Cookie:&quot;</span><span class="p">))</span>
327
 
<span class="go">Cookie: rocky=road</span>
328
 
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s2">&quot;chips=ahoy; vienna=finger&quot;</span><span class="p">)</span> <span class="c1"># load from a string (HTTP header)</span>
330
 
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s1">&#39;keebler=&quot;E=everybody; L=</span><span class="se">\\</span><span class="s1">&quot;Loves</span><span class="se">\\</span><span class="s1">&quot;; fudge=</span><span class="se">\\</span><span class="s1">012;&quot;;&#39;</span><span class="p">)</span>
335
 
<span class="gp">&gt;&gt;&gt; </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=&quot;E=everybody; L=\&quot;Loves\&quot;; fudge=\012;&quot;</span>
337
 
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">[</span><span class="s2">&quot;oreo&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;doublestuff&quot;</span>
339
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">[</span><span class="s2">&quot;oreo&quot;</span><span class="p">][</span><span class="s2">&quot;path&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;/&quot;</span>
340
 
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">[</span><span class="s2">&quot;twix&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;none for you&quot;</span>
344
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">[</span><span class="s2">&quot;twix&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>
345
 
<span class="go">&#39;none for you&#39;</span>
346
 
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">[</span><span class="s2">&quot;number&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">7</span> <span class="c1"># equivalent to C[&quot;number&quot;] = str(7)</span>
348
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">[</span><span class="s2">&quot;string&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;seven&quot;</span>
349
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">[</span><span class="s2">&quot;number&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>
350
 
<span class="go">&#39;7&#39;</span>
351
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">[</span><span class="s2">&quot;string&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value</span>
352
 
<span class="go">&#39;seven&#39;</span>
353
 
<span class="gp">&gt;&gt;&gt; </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>
356
 
</pre></div>
357
 
</div>
358
 
</div>
359
 
</div>
360
 
 
361
 
 
362
 
          </div>
363
 
        </div>
364
 
      </div>
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>
368
 
  <ul>
369
 
<li><a class="reference internal" href="#">21.23. <code class="docutils literal"><span class="pre">http.cookies</span></code> &#8212; 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>
373
 
</ul>
374
 
</li>
375
 
</ul>
376
 
 
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> &#8212; HTTP servers</a></p>
380
 
  <h4>Next topic</h4>
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> &#8212; Cookie handling for HTTP clients</a></p>
383
 
<h3>This Page</h3>
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>
388
 
</ul>
389
 
 
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" />
397
 
    </form>
398
 
    <p class="searchtip" style="font-size: 90%">
399
 
    Enter search terms or a module, class or function name.
400
 
    </p>
401
 
</div>
402
 
<script type="text/javascript">$('#searchbox').show(0);</script>
403
 
        </div>
404
 
      </div>
405
 
      <div class="clearer"></div>
406
 
    </div>  
407
 
    <div class="related" role="navigation" aria-label="related navigation">
408
 
      <h3>Navigation</h3>
409
 
      <ul>
410
 
        <li class="right" style="margin-right: 10px">
411
 
          <a href="../genindex.html" title="General Index"
412
 
             >index</a></li>
413
 
        <li class="right" >
414
 
          <a href="../py-modindex.html" title="Python Module Index"
415
 
             >modules</a> |</li>
416
 
        <li class="right" >
417
 
          <a href="http.cookiejar.html" title="21.24. http.cookiejar — Cookie handling for HTTP clients"
418
 
             >next</a> |</li>
419
 
        <li class="right" >
420
 
          <a href="http.server.html" title="21.22. http.server — HTTP servers"
421
 
             >previous</a> |</li>
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> &raquo;</li>
425
 
        <li>
426
 
          <span class="version_switcher_placeholder">3.5.1</span>
427
 
          <a href="../index.html">Documentation </a> &raquo;
428
 
        </li>
429
 
 
430
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
431
 
          <li class="nav-item nav-item-2"><a href="internet.html" >21. Internet Protocols and Support</a> &raquo;</li> 
432
 
      </ul>
433
 
    </div>  
434
 
    <div class="footer">
435
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
436
 
    <br />
437
 
    The Python Software Foundation is a non-profit corporation.
438
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
439
 
    <br />
440
 
    Last updated on Jan 22, 2016.
441
 
    <a href="../bugs.html">Found a bug</a>?
442
 
    <br />
443
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
444
 
    </div>
445
 
 
446
 
  </body>
447
 
</html>
 
 
b'\\ No newline at end of file'