diff options
Diffstat (limited to 'devdocs/python~3.12/library%2Fhttp.cookiejar.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Fhttp.cookiejar.html | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fhttp.cookiejar.html b/devdocs/python~3.12/library%2Fhttp.cookiejar.html new file mode 100644 index 00000000..a5d85980 --- /dev/null +++ b/devdocs/python~3.12/library%2Fhttp.cookiejar.html @@ -0,0 +1,241 @@ + <span id="http-cookiejar-cookie-handling-for-http-clients"></span><h1>http.cookiejar — Cookie handling for HTTP clients</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/http/cookiejar.py">Lib/http/cookiejar.py</a></p> <p>The <a class="reference internal" href="#module-http.cookiejar" title="http.cookiejar: Classes for automatic handling of HTTP cookies."><code>http.cookiejar</code></a> module defines classes for automatic handling of HTTP cookies. It is useful for accessing web sites that require small pieces of data – <em class="dfn">cookies</em> – to be set on the client machine by an HTTP response from a web server, and then returned to the server in later HTTP requests.</p> <p>Both the regular Netscape cookie protocol and the protocol defined by <span class="target" id="index-0"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> are handled. RFC 2965 handling is switched off by default. <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2109.html"><strong>RFC 2109</strong></a> cookies are parsed as Netscape cookies and subsequently treated either as Netscape or RFC 2965 cookies according to the ‘policy’ in effect. Note that the great majority of cookies on the internet are Netscape cookies. <a class="reference internal" href="#module-http.cookiejar" title="http.cookiejar: Classes for automatic handling of HTTP cookies."><code>http.cookiejar</code></a> attempts to follow the de-facto Netscape cookie protocol (which differs substantially from that set out in the original Netscape specification), including taking note of the <code>max-age</code> and <code>port</code> cookie-attributes introduced with RFC 2965.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The various named parameters found in <em class="mailheader">Set-Cookie</em> and <em class="mailheader">Set-Cookie2</em> headers (eg. <code>domain</code> and <code>expires</code>) are conventionally referred to as <em class="dfn">attributes</em>. To distinguish them from Python attributes, the documentation for this module uses the term <em class="dfn">cookie-attribute</em> instead.</p> </div> <p>The module defines the following exception:</p> <dl class="py exception"> <dt class="sig sig-object py" id="http.cookiejar.LoadError"> +<code>exception http.cookiejar.LoadError</code> </dt> <dd> +<p>Instances of <a class="reference internal" href="#http.cookiejar.FileCookieJar" title="http.cookiejar.FileCookieJar"><code>FileCookieJar</code></a> raise this exception on failure to load cookies from a file. <a class="reference internal" href="#http.cookiejar.LoadError" title="http.cookiejar.LoadError"><code>LoadError</code></a> is a subclass of <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="#http.cookiejar.LoadError" title="http.cookiejar.LoadError"><code>LoadError</code></a> used to be a subtype of <a class="reference internal" href="exceptions#IOError" title="IOError"><code>IOError</code></a>, which is now an alias of <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a>.</p> </div> </dd> +</dl> <p>The following classes are provided:</p> <dl class="py class"> <dt class="sig sig-object py" id="http.cookiejar.CookieJar"> +<code>class http.cookiejar.CookieJar(policy=None)</code> </dt> <dd> +<p><em>policy</em> is an object implementing the <a class="reference internal" href="#http.cookiejar.CookiePolicy" title="http.cookiejar.CookiePolicy"><code>CookiePolicy</code></a> interface.</p> <p>The <a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a> class stores HTTP cookies. It extracts cookies from HTTP requests, and returns them in HTTP responses. <a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a> instances automatically expire contained cookies when necessary. Subclasses are also responsible for storing and retrieving cookies from a file or database.</p> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py" id="http.cookiejar.FileCookieJar"> +<code>class http.cookiejar.FileCookieJar(filename=None, delayload=None, policy=None)</code> </dt> <dd> +<p><em>policy</em> is an object implementing the <a class="reference internal" href="#http.cookiejar.CookiePolicy" title="http.cookiejar.CookiePolicy"><code>CookiePolicy</code></a> interface. For the other arguments, see the documentation for the corresponding attributes.</p> <p>A <a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a> which can load cookies from, and perhaps save cookies to, a file on disk. Cookies are <strong>NOT</strong> loaded from the named file until either the <a class="reference internal" href="#http.cookiejar.FileCookieJar.load" title="http.cookiejar.FileCookieJar.load"><code>load()</code></a> or <a class="reference internal" href="#http.cookiejar.FileCookieJar.revert" title="http.cookiejar.FileCookieJar.revert"><code>revert()</code></a> method is called. Subclasses of this class are documented in section <a class="reference internal" href="#file-cookie-jar-classes"><span class="std std-ref">FileCookieJar subclasses and co-operation with web browsers</span></a>.</p> <p>This should not be initialized directly – use its subclasses below instead.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>The filename parameter supports a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py" id="http.cookiejar.CookiePolicy"> +<code>class http.cookiejar.CookiePolicy</code> </dt> <dd> +<p>This class is responsible for deciding whether each cookie should be accepted from / returned to the server.</p> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy"> +<code>class http.cookiejar.DefaultCookiePolicy(blocked_domains=None, allowed_domains=None, netscape=True, rfc2965=False, rfc2109_as_netscape=None, hide_cookie2=False, strict_domain=False, strict_rfc2965_unverifiable=True, strict_ns_unverifiable=False, strict_ns_domain=DefaultCookiePolicy.DomainLiberal, strict_ns_set_initial_dollar=False, strict_ns_set_path=False, secure_protocols=('https', 'wss'))</code> </dt> <dd> +<p>Constructor arguments should be passed as keyword arguments only. <em>blocked_domains</em> is a sequence of domain names that we never accept cookies from, nor return cookies to. <em>allowed_domains</em> if not <a class="reference internal" href="constants#None" title="None"><code>None</code></a>, this is a sequence of the only domains for which we accept and return cookies. <em>secure_protocols</em> is a sequence of protocols for which secure cookies can be added to. By default <em>https</em> and <em>wss</em> (secure websocket) are considered secure protocols. For all other arguments, see the documentation for <a class="reference internal" href="#http.cookiejar.CookiePolicy" title="http.cookiejar.CookiePolicy"><code>CookiePolicy</code></a> and <a class="reference internal" href="#http.cookiejar.DefaultCookiePolicy" title="http.cookiejar.DefaultCookiePolicy"><code>DefaultCookiePolicy</code></a> objects.</p> <p><a class="reference internal" href="#http.cookiejar.DefaultCookiePolicy" title="http.cookiejar.DefaultCookiePolicy"><code>DefaultCookiePolicy</code></a> implements the standard accept / reject rules for Netscape and <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> cookies. By default, <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2109.html"><strong>RFC 2109</strong></a> cookies (ie. cookies received in a <em class="mailheader">Set-Cookie</em> header with a version cookie-attribute of 1) are treated according to the RFC 2965 rules. However, if RFC 2965 handling is turned off or <a class="reference internal" href="#http.cookiejar.DefaultCookiePolicy.rfc2109_as_netscape" title="http.cookiejar.DefaultCookiePolicy.rfc2109_as_netscape"><code>rfc2109_as_netscape</code></a> is <code>True</code>, RFC 2109 cookies are ‘downgraded’ by the <a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a> instance to Netscape cookies, by setting the <code>version</code> attribute of the <a class="reference internal" href="#http.cookiejar.Cookie" title="http.cookiejar.Cookie"><code>Cookie</code></a> instance to 0. <a class="reference internal" href="#http.cookiejar.DefaultCookiePolicy" title="http.cookiejar.DefaultCookiePolicy"><code>DefaultCookiePolicy</code></a> also provides some parameters to allow some fine-tuning of policy.</p> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py" id="http.cookiejar.Cookie"> +<code>class http.cookiejar.Cookie</code> </dt> <dd> +<p>This class represents Netscape, <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2109.html"><strong>RFC 2109</strong></a> and <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> cookies. It is not expected that users of <a class="reference internal" href="#module-http.cookiejar" title="http.cookiejar: Classes for automatic handling of HTTP cookies."><code>http.cookiejar</code></a> construct their own <a class="reference internal" href="#http.cookiejar.Cookie" title="http.cookiejar.Cookie"><code>Cookie</code></a> instances. Instead, if necessary, call <code>make_cookies()</code> on a <a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a> instance.</p> </dd> +</dl> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt> +<code>Module</code> <a class="reference internal" href="urllib.request#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code>urllib.request</code></a> +</dt> +<dd> +<p>URL opening with automatic cookie handling.</p> </dd> <dt> +<code>Module</code> <a class="reference internal" href="http.cookies#module-http.cookies" title="http.cookies: Support for HTTP state management (cookies)."><code>http.cookies</code></a> +</dt> +<dd> +<p>HTTP cookie classes, principally useful for server-side code. The <a class="reference internal" href="#module-http.cookiejar" title="http.cookiejar: Classes for automatic handling of HTTP cookies."><code>http.cookiejar</code></a> and <a class="reference internal" href="http.cookies#module-http.cookies" title="http.cookies: Support for HTTP state management (cookies)."><code>http.cookies</code></a> modules do not depend on each other.</p> </dd> <dt><a class="reference external" href="https://curl.se/rfc/cookie_spec.html">https://curl.se/rfc/cookie_spec.html</a></dt> +<dd> +<p>The specification of the original Netscape cookie protocol. Though this is still the dominant protocol, the ‘Netscape cookie protocol’ implemented by all the major browsers (and <a class="reference internal" href="#module-http.cookiejar" title="http.cookiejar: Classes for automatic handling of HTTP cookies."><code>http.cookiejar</code></a>) only bears a passing resemblance to the one sketched out in <code>cookie_spec.html</code>.</p> </dd> <dt> +<span class="target" id="index-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2109.html"><strong>RFC 2109</strong></a> - HTTP State Management Mechanism</dt> +<dd> +<p>Obsoleted by <span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a>. Uses <em class="mailheader">Set-Cookie</em> with version=1.</p> </dd> <dt> +<span class="target" id="index-8"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> - HTTP State Management Mechanism</dt> +<dd> +<p>The Netscape protocol with the bugs fixed. Uses <em class="mailheader">Set-Cookie2</em> in place of <em class="mailheader">Set-Cookie</em>. Not widely used.</p> </dd> <dt><a class="reference external" href="http://kristol.org/cookie/errata.html">http://kristol.org/cookie/errata.html</a></dt> +<dd> +<p>Unfinished errata to <span class="target" id="index-9"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a>.</p> </dd> </dl> <p><span class="target" id="index-10"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2964.html"><strong>RFC 2964</strong></a> - Use of HTTP State Management</p> </div> <section id="cookiejar-and-filecookiejar-objects"> <span id="cookie-jar-objects"></span><h2>CookieJar and FileCookieJar Objects</h2> <p><a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a> objects support the <a class="reference internal" href="../glossary#term-iterator"><span class="xref std std-term">iterator</span></a> protocol for iterating over contained <a class="reference internal" href="#http.cookiejar.Cookie" title="http.cookiejar.Cookie"><code>Cookie</code></a> objects.</p> <p><a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a> has the following methods:</p> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.CookieJar.add_cookie_header"> +<code>CookieJar.add_cookie_header(request)</code> </dt> <dd> +<p>Add correct <em class="mailheader">Cookie</em> header to <em>request</em>.</p> <p>If policy allows (ie. the <code>rfc2965</code> and <code>hide_cookie2</code> attributes of the <a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a>’s <a class="reference internal" href="#http.cookiejar.CookiePolicy" title="http.cookiejar.CookiePolicy"><code>CookiePolicy</code></a> instance are true and false respectively), the <em class="mailheader">Cookie2</em> header is also added when appropriate.</p> <p>The <em>request</em> object (usually a <a class="reference internal" href="urllib.request#urllib.request.Request" title="urllib.request.Request"><code>urllib.request.Request</code></a> instance) must support the methods <code>get_full_url()</code>, <code>has_header()</code>, <code>get_header()</code>, <code>header_items()</code>, <code>add_unredirected_header()</code> and the attributes <code>host</code>, <code>type</code>, <code>unverifiable</code> and <code>origin_req_host</code> as documented by <a class="reference internal" href="urllib.request#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code>urllib.request</code></a>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span><em>request</em> object needs <code>origin_req_host</code> attribute. Dependency on a deprecated method <code>get_origin_req_host()</code> has been removed.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.CookieJar.extract_cookies"> +<code>CookieJar.extract_cookies(response, request)</code> </dt> <dd> +<p>Extract cookies from HTTP <em>response</em> and store them in the <a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a>, where allowed by policy.</p> <p>The <a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a> will look for allowable <em class="mailheader">Set-Cookie</em> and <em class="mailheader">Set-Cookie2</em> headers in the <em>response</em> argument, and store cookies as appropriate (subject to the <a class="reference internal" href="#http.cookiejar.CookiePolicy.set_ok" title="http.cookiejar.CookiePolicy.set_ok"><code>CookiePolicy.set_ok()</code></a> method’s approval).</p> <p>The <em>response</em> object (usually the result of a call to <a class="reference internal" href="urllib.request#urllib.request.urlopen" title="urllib.request.urlopen"><code>urllib.request.urlopen()</code></a>, or similar) should support an <code>info()</code> method, which returns an <a class="reference internal" href="email.compat32-message#email.message.Message" title="email.message.Message"><code>email.message.Message</code></a> instance.</p> <p>The <em>request</em> object (usually a <a class="reference internal" href="urllib.request#urllib.request.Request" title="urllib.request.Request"><code>urllib.request.Request</code></a> instance) must support the method <code>get_full_url()</code> and the attributes <code>host</code>, <code>unverifiable</code> and <code>origin_req_host</code>, as documented by <a class="reference internal" href="urllib.request#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code>urllib.request</code></a>. The request is used to set default values for cookie-attributes as well as for checking that the cookie is allowed to be set.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span><em>request</em> object needs <code>origin_req_host</code> attribute. Dependency on a deprecated method <code>get_origin_req_host()</code> has been removed.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.CookieJar.set_policy"> +<code>CookieJar.set_policy(policy)</code> </dt> <dd> +<p>Set the <a class="reference internal" href="#http.cookiejar.CookiePolicy" title="http.cookiejar.CookiePolicy"><code>CookiePolicy</code></a> instance to be used.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.CookieJar.make_cookies"> +<code>CookieJar.make_cookies(response, request)</code> </dt> <dd> +<p>Return sequence of <a class="reference internal" href="#http.cookiejar.Cookie" title="http.cookiejar.Cookie"><code>Cookie</code></a> objects extracted from <em>response</em> object.</p> <p>See the documentation for <a class="reference internal" href="#http.cookiejar.CookieJar.extract_cookies" title="http.cookiejar.CookieJar.extract_cookies"><code>extract_cookies()</code></a> for the interfaces required of the <em>response</em> and <em>request</em> arguments.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.CookieJar.set_cookie_if_ok"> +<code>CookieJar.set_cookie_if_ok(cookie, request)</code> </dt> <dd> +<p>Set a <a class="reference internal" href="#http.cookiejar.Cookie" title="http.cookiejar.Cookie"><code>Cookie</code></a> if policy says it’s OK to do so.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.CookieJar.set_cookie"> +<code>CookieJar.set_cookie(cookie)</code> </dt> <dd> +<p>Set a <a class="reference internal" href="#http.cookiejar.Cookie" title="http.cookiejar.Cookie"><code>Cookie</code></a>, without checking with policy to see whether or not it should be set.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.CookieJar.clear"> +<code>CookieJar.clear([domain[, path[, name]]])</code> </dt> <dd> +<p>Clear some cookies.</p> <p>If invoked without arguments, clear all cookies. If given a single argument, only cookies belonging to that <em>domain</em> will be removed. If given two arguments, cookies belonging to the specified <em>domain</em> and URL <em>path</em> are removed. If given three arguments, then the cookie with the specified <em>domain</em>, <em>path</em> and <em>name</em> is removed.</p> <p>Raises <a class="reference internal" href="exceptions#KeyError" title="KeyError"><code>KeyError</code></a> if no matching cookie exists.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.CookieJar.clear_session_cookies"> +<code>CookieJar.clear_session_cookies()</code> </dt> <dd> +<p>Discard all session cookies.</p> <p>Discards all contained cookies that have a true <code>discard</code> attribute (usually because they had either no <code>max-age</code> or <code>expires</code> cookie-attribute, or an explicit <code>discard</code> cookie-attribute). For interactive browsers, the end of a session usually corresponds to closing the browser window.</p> <p>Note that the <code>save()</code> method won’t save session cookies anyway, unless you ask otherwise by passing a true <em>ignore_discard</em> argument.</p> </dd> +</dl> <p><a class="reference internal" href="#http.cookiejar.FileCookieJar" title="http.cookiejar.FileCookieJar"><code>FileCookieJar</code></a> implements the following additional methods:</p> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.FileCookieJar.save"> +<code>FileCookieJar.save(filename=None, ignore_discard=False, ignore_expires=False)</code> </dt> <dd> +<p>Save cookies to a file.</p> <p>This base class raises <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a>. Subclasses may leave this method unimplemented.</p> <p><em>filename</em> is the name of file in which to save cookies. If <em>filename</em> is not specified, <code>self.filename</code> is used (whose default is the value passed to the constructor, if any); if <code>self.filename</code> is <a class="reference internal" href="constants#None" title="None"><code>None</code></a>, <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised.</p> <p><em>ignore_discard</em>: save even cookies set to be discarded. <em>ignore_expires</em>: save even cookies that have expired</p> <p>The file is overwritten if it already exists, thus wiping all the cookies it contains. Saved cookies can be restored later using the <a class="reference internal" href="#http.cookiejar.FileCookieJar.load" title="http.cookiejar.FileCookieJar.load"><code>load()</code></a> or <a class="reference internal" href="#http.cookiejar.FileCookieJar.revert" title="http.cookiejar.FileCookieJar.revert"><code>revert()</code></a> methods.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.FileCookieJar.load"> +<code>FileCookieJar.load(filename=None, ignore_discard=False, ignore_expires=False)</code> </dt> <dd> +<p>Load cookies from a file.</p> <p>Old cookies are kept unless overwritten by newly loaded ones.</p> <p>Arguments are as for <a class="reference internal" href="#http.cookiejar.FileCookieJar.save" title="http.cookiejar.FileCookieJar.save"><code>save()</code></a>.</p> <p>The named file must be in the format understood by the class, or <a class="reference internal" href="#http.cookiejar.LoadError" title="http.cookiejar.LoadError"><code>LoadError</code></a> will be raised. Also, <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> may be raised, for example if the file does not exist.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="exceptions#IOError" title="IOError"><code>IOError</code></a> used to be raised, it is now an alias of <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a>.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.FileCookieJar.revert"> +<code>FileCookieJar.revert(filename=None, ignore_discard=False, ignore_expires=False)</code> </dt> <dd> +<p>Clear all cookies and reload cookies from a saved file.</p> <p><a class="reference internal" href="#http.cookiejar.FileCookieJar.revert" title="http.cookiejar.FileCookieJar.revert"><code>revert()</code></a> can raise the same exceptions as <a class="reference internal" href="#http.cookiejar.FileCookieJar.load" title="http.cookiejar.FileCookieJar.load"><code>load()</code></a>. If there is a failure, the object’s state will not be altered.</p> </dd> +</dl> <p><a class="reference internal" href="#http.cookiejar.FileCookieJar" title="http.cookiejar.FileCookieJar"><code>FileCookieJar</code></a> instances have the following public attributes:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.FileCookieJar.filename"> +<code>FileCookieJar.filename</code> </dt> <dd> +<p>Filename of default file in which to keep cookies. This attribute may be assigned to.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.FileCookieJar.delayload"> +<code>FileCookieJar.delayload</code> </dt> <dd> +<p>If true, load cookies lazily from disk. This attribute should not be assigned to. This is only a hint, since this only affects performance, not behaviour (unless the cookies on disk are changing). A <a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a> object may ignore it. None of the <a class="reference internal" href="#http.cookiejar.FileCookieJar" title="http.cookiejar.FileCookieJar"><code>FileCookieJar</code></a> classes included in the standard library lazily loads cookies.</p> </dd> +</dl> </section> <section id="filecookiejar-subclasses-and-co-operation-with-web-browsers"> <span id="file-cookie-jar-classes"></span><h2>FileCookieJar subclasses and co-operation with web browsers</h2> <p>The following <a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a> subclasses are provided for reading and writing.</p> <dl class="py class"> <dt class="sig sig-object py" id="http.cookiejar.MozillaCookieJar"> +<code>class http.cookiejar.MozillaCookieJar(filename=None, delayload=None, policy=None)</code> </dt> <dd> +<p>A <a class="reference internal" href="#http.cookiejar.FileCookieJar" title="http.cookiejar.FileCookieJar"><code>FileCookieJar</code></a> that can load from and save cookies to disk in the Mozilla <code>cookies.txt</code> file format (which is also used by curl and the Lynx and Netscape browsers).</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This loses information about <span class="target" id="index-11"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> cookies, and also about newer or non-standard cookie-attributes such as <code>port</code>.</p> </div> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>Back up your cookies before saving if you have cookies whose loss / corruption would be inconvenient (there are some subtleties which may lead to slight changes in the file over a load / save round-trip).</p> </div> <p>Also note that cookies saved while Mozilla is running will get clobbered by Mozilla.</p> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py" id="http.cookiejar.LWPCookieJar"> +<code>class http.cookiejar.LWPCookieJar(filename=None, delayload=None, policy=None)</code> </dt> <dd> +<p>A <a class="reference internal" href="#http.cookiejar.FileCookieJar" title="http.cookiejar.FileCookieJar"><code>FileCookieJar</code></a> that can load from and save cookies to disk in format compatible with the libwww-perl library’s <code>Set-Cookie3</code> file format. This is convenient if you want to store cookies in a human-readable file.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>The filename parameter supports a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd> +</dl> </section> <section id="cookiepolicy-objects"> <span id="cookie-policy-objects"></span><h2>CookiePolicy Objects</h2> <p>Objects implementing the <a class="reference internal" href="#http.cookiejar.CookiePolicy" title="http.cookiejar.CookiePolicy"><code>CookiePolicy</code></a> interface have the following methods:</p> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.CookiePolicy.set_ok"> +<code>CookiePolicy.set_ok(cookie, request)</code> </dt> <dd> +<p>Return boolean value indicating whether cookie should be accepted from server.</p> <p><em>cookie</em> is a <a class="reference internal" href="#http.cookiejar.Cookie" title="http.cookiejar.Cookie"><code>Cookie</code></a> instance. <em>request</em> is an object implementing the interface defined by the documentation for <a class="reference internal" href="#http.cookiejar.CookieJar.extract_cookies" title="http.cookiejar.CookieJar.extract_cookies"><code>CookieJar.extract_cookies()</code></a>.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.CookiePolicy.return_ok"> +<code>CookiePolicy.return_ok(cookie, request)</code> </dt> <dd> +<p>Return boolean value indicating whether cookie should be returned to server.</p> <p><em>cookie</em> is a <a class="reference internal" href="#http.cookiejar.Cookie" title="http.cookiejar.Cookie"><code>Cookie</code></a> instance. <em>request</em> is an object implementing the interface defined by the documentation for <a class="reference internal" href="#http.cookiejar.CookieJar.add_cookie_header" title="http.cookiejar.CookieJar.add_cookie_header"><code>CookieJar.add_cookie_header()</code></a>.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.CookiePolicy.domain_return_ok"> +<code>CookiePolicy.domain_return_ok(domain, request)</code> </dt> <dd> +<p>Return <code>False</code> if cookies should not be returned, given cookie domain.</p> <p>This method is an optimization. It removes the need for checking every cookie with a particular domain (which might involve reading many files). Returning true from <a class="reference internal" href="#http.cookiejar.CookiePolicy.domain_return_ok" title="http.cookiejar.CookiePolicy.domain_return_ok"><code>domain_return_ok()</code></a> and <a class="reference internal" href="#http.cookiejar.CookiePolicy.path_return_ok" title="http.cookiejar.CookiePolicy.path_return_ok"><code>path_return_ok()</code></a> leaves all the work to <a class="reference internal" href="#http.cookiejar.CookiePolicy.return_ok" title="http.cookiejar.CookiePolicy.return_ok"><code>return_ok()</code></a>.</p> <p>If <a class="reference internal" href="#http.cookiejar.CookiePolicy.domain_return_ok" title="http.cookiejar.CookiePolicy.domain_return_ok"><code>domain_return_ok()</code></a> returns true for the cookie domain, <a class="reference internal" href="#http.cookiejar.CookiePolicy.path_return_ok" title="http.cookiejar.CookiePolicy.path_return_ok"><code>path_return_ok()</code></a> is called for the cookie path. Otherwise, <a class="reference internal" href="#http.cookiejar.CookiePolicy.path_return_ok" title="http.cookiejar.CookiePolicy.path_return_ok"><code>path_return_ok()</code></a> and <a class="reference internal" href="#http.cookiejar.CookiePolicy.return_ok" title="http.cookiejar.CookiePolicy.return_ok"><code>return_ok()</code></a> are never called for that cookie domain. If <a class="reference internal" href="#http.cookiejar.CookiePolicy.path_return_ok" title="http.cookiejar.CookiePolicy.path_return_ok"><code>path_return_ok()</code></a> returns true, <a class="reference internal" href="#http.cookiejar.CookiePolicy.return_ok" title="http.cookiejar.CookiePolicy.return_ok"><code>return_ok()</code></a> is called with the <a class="reference internal" href="#http.cookiejar.Cookie" title="http.cookiejar.Cookie"><code>Cookie</code></a> object itself for a full check. Otherwise, <a class="reference internal" href="#http.cookiejar.CookiePolicy.return_ok" title="http.cookiejar.CookiePolicy.return_ok"><code>return_ok()</code></a> is never called for that cookie path.</p> <p>Note that <a class="reference internal" href="#http.cookiejar.CookiePolicy.domain_return_ok" title="http.cookiejar.CookiePolicy.domain_return_ok"><code>domain_return_ok()</code></a> is called for every <em>cookie</em> domain, not just for the <em>request</em> domain. For example, the function might be called with both <code>".example.com"</code> and <code>"www.example.com"</code> if the request domain is <code>"www.example.com"</code>. The same goes for <a class="reference internal" href="#http.cookiejar.CookiePolicy.path_return_ok" title="http.cookiejar.CookiePolicy.path_return_ok"><code>path_return_ok()</code></a>.</p> <p>The <em>request</em> argument is as documented for <a class="reference internal" href="#http.cookiejar.CookiePolicy.return_ok" title="http.cookiejar.CookiePolicy.return_ok"><code>return_ok()</code></a>.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.CookiePolicy.path_return_ok"> +<code>CookiePolicy.path_return_ok(path, request)</code> </dt> <dd> +<p>Return <code>False</code> if cookies should not be returned, given cookie path.</p> <p>See the documentation for <a class="reference internal" href="#http.cookiejar.CookiePolicy.domain_return_ok" title="http.cookiejar.CookiePolicy.domain_return_ok"><code>domain_return_ok()</code></a>.</p> </dd> +</dl> <p>In addition to implementing the methods above, implementations of the <a class="reference internal" href="#http.cookiejar.CookiePolicy" title="http.cookiejar.CookiePolicy"><code>CookiePolicy</code></a> interface must also supply the following attributes, indicating which protocols should be used, and how. All of these attributes may be assigned to.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.CookiePolicy.netscape"> +<code>CookiePolicy.netscape</code> </dt> <dd> +<p>Implement Netscape protocol.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.CookiePolicy.rfc2965"> +<code>CookiePolicy.rfc2965</code> </dt> <dd> +<p>Implement <span class="target" id="index-12"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> protocol.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.CookiePolicy.hide_cookie2"> +<code>CookiePolicy.hide_cookie2</code> </dt> <dd> +<p>Don’t add <em class="mailheader">Cookie2</em> header to requests (the presence of this header indicates to the server that we understand <span class="target" id="index-13"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> cookies).</p> </dd> +</dl> <p>The most useful way to define a <a class="reference internal" href="#http.cookiejar.CookiePolicy" title="http.cookiejar.CookiePolicy"><code>CookiePolicy</code></a> class is by subclassing from <a class="reference internal" href="#http.cookiejar.DefaultCookiePolicy" title="http.cookiejar.DefaultCookiePolicy"><code>DefaultCookiePolicy</code></a> and overriding some or all of the methods above. <a class="reference internal" href="#http.cookiejar.CookiePolicy" title="http.cookiejar.CookiePolicy"><code>CookiePolicy</code></a> itself may be used as a ‘null policy’ to allow setting and receiving any and all cookies (this is unlikely to be useful).</p> </section> <section id="defaultcookiepolicy-objects"> <span id="default-cookie-policy-objects"></span><h2>DefaultCookiePolicy Objects</h2> <p>Implements the standard rules for accepting and returning cookies.</p> <p>Both <span class="target" id="index-14"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> and Netscape cookies are covered. RFC 2965 handling is switched off by default.</p> <p>The easiest way to provide your own policy is to override this class and call its methods in your overridden implementations before adding your own additional checks:</p> <pre data-language="python">import http.cookiejar +class MyCookiePolicy(http.cookiejar.DefaultCookiePolicy): + def set_ok(self, cookie, request): + if not http.cookiejar.DefaultCookiePolicy.set_ok(self, cookie, request): + return False + if i_dont_want_to_store_this_cookie(cookie): + return False + return True +</pre> <p>In addition to the features required to implement the <a class="reference internal" href="#http.cookiejar.CookiePolicy" title="http.cookiejar.CookiePolicy"><code>CookiePolicy</code></a> interface, this class allows you to block and allow domains from setting and receiving cookies. There are also some strictness switches that allow you to tighten up the rather loose Netscape protocol rules a little bit (at the cost of blocking some benign cookies).</p> <p>A domain blocklist and allowlist is provided (both off by default). Only domains not in the blocklist and present in the allowlist (if the allowlist is active) participate in cookie setting and returning. Use the <em>blocked_domains</em> constructor argument, and <code>blocked_domains()</code> and <code>set_blocked_domains()</code> methods (and the corresponding argument and methods for <em>allowed_domains</em>). If you set an allowlist, you can turn it off again by setting it to <a class="reference internal" href="constants#None" title="None"><code>None</code></a>.</p> <p>Domains in block or allow lists that do not start with a dot must equal the cookie domain to be matched. For example, <code>"example.com"</code> matches a blocklist entry of <code>"example.com"</code>, but <code>"www.example.com"</code> does not. Domains that do start with a dot are matched by more specific domains too. For example, both <code>"www.example.com"</code> and <code>"www.coyote.example.com"</code> match <code>".example.com"</code> (but <code>"example.com"</code> itself does not). IP addresses are an exception, and must match exactly. For example, if blocked_domains contains <code>"192.168.1.2"</code> and <code>".168.1.2"</code>, 192.168.1.2 is blocked, but 193.168.1.2 is not.</p> <p><a class="reference internal" href="#http.cookiejar.DefaultCookiePolicy" title="http.cookiejar.DefaultCookiePolicy"><code>DefaultCookiePolicy</code></a> implements the following additional methods:</p> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.blocked_domains"> +<code>DefaultCookiePolicy.blocked_domains()</code> </dt> <dd> +<p>Return the sequence of blocked domains (as a tuple).</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.set_blocked_domains"> +<code>DefaultCookiePolicy.set_blocked_domains(blocked_domains)</code> </dt> <dd> +<p>Set the sequence of blocked domains.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.is_blocked"> +<code>DefaultCookiePolicy.is_blocked(domain)</code> </dt> <dd> +<p>Return <code>True</code> if <em>domain</em> is on the blocklist for setting or receiving cookies.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.allowed_domains"> +<code>DefaultCookiePolicy.allowed_domains()</code> </dt> <dd> +<p>Return <a class="reference internal" href="constants#None" title="None"><code>None</code></a>, or the sequence of allowed domains (as a tuple).</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.set_allowed_domains"> +<code>DefaultCookiePolicy.set_allowed_domains(allowed_domains)</code> </dt> <dd> +<p>Set the sequence of allowed domains, or <a class="reference internal" href="constants#None" title="None"><code>None</code></a>.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.is_not_allowed"> +<code>DefaultCookiePolicy.is_not_allowed(domain)</code> </dt> <dd> +<p>Return <code>True</code> if <em>domain</em> is not on the allowlist for setting or receiving cookies.</p> </dd> +</dl> <p><a class="reference internal" href="#http.cookiejar.DefaultCookiePolicy" title="http.cookiejar.DefaultCookiePolicy"><code>DefaultCookiePolicy</code></a> instances have the following attributes, which are all initialised from the constructor arguments of the same name, and which may all be assigned to.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.rfc2109_as_netscape"> +<code>DefaultCookiePolicy.rfc2109_as_netscape</code> </dt> <dd> +<p>If true, request that the <a class="reference internal" href="#http.cookiejar.CookieJar" title="http.cookiejar.CookieJar"><code>CookieJar</code></a> instance downgrade <span class="target" id="index-15"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2109.html"><strong>RFC 2109</strong></a> cookies (ie. cookies received in a <em class="mailheader">Set-Cookie</em> header with a version cookie-attribute of 1) to Netscape cookies by setting the version attribute of the <a class="reference internal" href="#http.cookiejar.Cookie" title="http.cookiejar.Cookie"><code>Cookie</code></a> instance to 0. The default value is <a class="reference internal" href="constants#None" title="None"><code>None</code></a>, in which case RFC 2109 cookies are downgraded if and only if <span class="target" id="index-16"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> handling is turned off. Therefore, RFC 2109 cookies are downgraded by default.</p> </dd> +</dl> <p>General strictness switches:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.strict_domain"> +<code>DefaultCookiePolicy.strict_domain</code> </dt> <dd> +<p>Don’t allow sites to set two-component domains with country-code top-level domains like <code>.co.uk</code>, <code>.gov.uk</code>, <code>.co.nz</code>.etc. This is far from perfect and isn’t guaranteed to work!</p> </dd> +</dl> <p><span class="target" id="index-17"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> protocol strictness switches:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.strict_rfc2965_unverifiable"> +<code>DefaultCookiePolicy.strict_rfc2965_unverifiable</code> </dt> <dd> +<p>Follow <span class="target" id="index-18"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> rules on unverifiable transactions (usually, an unverifiable transaction is one resulting from a redirect or a request for an image hosted on another site). If this is false, cookies are <em>never</em> blocked on the basis of verifiability</p> </dd> +</dl> <p>Netscape protocol strictness switches:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.strict_ns_unverifiable"> +<code>DefaultCookiePolicy.strict_ns_unverifiable</code> </dt> <dd> +<p>Apply <span class="target" id="index-19"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> rules on unverifiable transactions even to Netscape cookies.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.strict_ns_domain"> +<code>DefaultCookiePolicy.strict_ns_domain</code> </dt> <dd> +<p>Flags indicating how strict to be with domain-matching rules for Netscape cookies. See below for acceptable values.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.strict_ns_set_initial_dollar"> +<code>DefaultCookiePolicy.strict_ns_set_initial_dollar</code> </dt> <dd> +<p>Ignore cookies in Set-Cookie: headers that have names starting with <code>'$'</code>.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.strict_ns_set_path"> +<code>DefaultCookiePolicy.strict_ns_set_path</code> </dt> <dd> +<p>Don’t allow setting cookies whose path doesn’t path-match request URI.</p> </dd> +</dl> <p><code>strict_ns_domain</code> is a collection of flags. Its value is constructed by or-ing together (for example, <code>DomainStrictNoDots|DomainStrictNonDomain</code> means both flags are set).</p> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.DomainStrictNoDots"> +<code>DefaultCookiePolicy.DomainStrictNoDots</code> </dt> <dd> +<p>When setting cookies, the ‘host prefix’ must not contain a dot (eg. <code>www.foo.bar.com</code> can’t set a cookie for <code>.bar.com</code>, because <code>www.foo</code> contains a dot).</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.DomainStrictNonDomain"> +<code>DefaultCookiePolicy.DomainStrictNonDomain</code> </dt> <dd> +<p>Cookies that did not explicitly specify a <code>domain</code> cookie-attribute can only be returned to a domain equal to the domain that set the cookie (eg. <code>spam.example.com</code> won’t be returned cookies from <code>example.com</code> that had no <code>domain</code> cookie-attribute).</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.DomainRFC2965Match"> +<code>DefaultCookiePolicy.DomainRFC2965Match</code> </dt> <dd> +<p>When setting cookies, require a full <span class="target" id="index-20"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> domain-match.</p> </dd> +</dl> <p>The following attributes are provided for convenience, and are the most useful combinations of the above flags:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.DomainLiberal"> +<code>DefaultCookiePolicy.DomainLiberal</code> </dt> <dd> +<p>Equivalent to 0 (ie. all of the above Netscape domain strictness flags switched off).</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.DefaultCookiePolicy.DomainStrict"> +<code>DefaultCookiePolicy.DomainStrict</code> </dt> <dd> +<p>Equivalent to <code>DomainStrictNoDots|DomainStrictNonDomain</code>.</p> </dd> +</dl> </section> <section id="cookie-objects"> <h2>Cookie Objects</h2> <p><a class="reference internal" href="#http.cookiejar.Cookie" title="http.cookiejar.Cookie"><code>Cookie</code></a> instances have Python attributes roughly corresponding to the standard cookie-attributes specified in the various cookie standards. The correspondence is not one-to-one, because there are complicated rules for assigning default values, because the <code>max-age</code> and <code>expires</code> cookie-attributes contain equivalent information, and because <span class="target" id="index-21"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2109.html"><strong>RFC 2109</strong></a> cookies may be ‘downgraded’ by <a class="reference internal" href="#module-http.cookiejar" title="http.cookiejar: Classes for automatic handling of HTTP cookies."><code>http.cookiejar</code></a> from version 1 to version 0 (Netscape) cookies.</p> <p>Assignment to these attributes should not be necessary other than in rare circumstances in a <a class="reference internal" href="#http.cookiejar.CookiePolicy" title="http.cookiejar.CookiePolicy"><code>CookiePolicy</code></a> method. The class does not enforce internal consistency, so you should know what you’re doing if you do that.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.version"> +<code>Cookie.version</code> </dt> <dd> +<p>Integer or <a class="reference internal" href="constants#None" title="None"><code>None</code></a>. Netscape cookies have <a class="reference internal" href="#http.cookiejar.Cookie.version" title="http.cookiejar.Cookie.version"><code>version</code></a> 0. <span class="target" id="index-22"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> and <span class="target" id="index-23"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2109.html"><strong>RFC 2109</strong></a> cookies have a <code>version</code> cookie-attribute of 1. However, note that <a class="reference internal" href="#module-http.cookiejar" title="http.cookiejar: Classes for automatic handling of HTTP cookies."><code>http.cookiejar</code></a> may ‘downgrade’ RFC 2109 cookies to Netscape cookies, in which case <a class="reference internal" href="#http.cookiejar.Cookie.version" title="http.cookiejar.Cookie.version"><code>version</code></a> is 0.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.name"> +<code>Cookie.name</code> </dt> <dd> +<p>Cookie name (a string).</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.value"> +<code>Cookie.value</code> </dt> <dd> +<p>Cookie value (a string), or <a class="reference internal" href="constants#None" title="None"><code>None</code></a>.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.port"> +<code>Cookie.port</code> </dt> <dd> +<p>String representing a port or a set of ports (eg. ‘80’, or ‘80,8080’), or <a class="reference internal" href="constants#None" title="None"><code>None</code></a>.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.path"> +<code>Cookie.path</code> </dt> <dd> +<p>Cookie path (a string, eg. <code>'/acme/rocket_launchers'</code>).</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.secure"> +<code>Cookie.secure</code> </dt> <dd> +<p><code>True</code> if cookie should only be returned over a secure connection.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.expires"> +<code>Cookie.expires</code> </dt> <dd> +<p>Integer expiry date in seconds since epoch, or <a class="reference internal" href="constants#None" title="None"><code>None</code></a>. See also the <a class="reference internal" href="#http.cookiejar.Cookie.is_expired" title="http.cookiejar.Cookie.is_expired"><code>is_expired()</code></a> method.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.discard"> +<code>Cookie.discard</code> </dt> <dd> +<p><code>True</code> if this is a session cookie.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.comment"> +<code>Cookie.comment</code> </dt> <dd> +<p>String comment from the server explaining the function of this cookie, or <a class="reference internal" href="constants#None" title="None"><code>None</code></a>.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.comment_url"> +<code>Cookie.comment_url</code> </dt> <dd> +<p>URL linking to a comment from the server explaining the function of this cookie, or <a class="reference internal" href="constants#None" title="None"><code>None</code></a>.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.rfc2109"> +<code>Cookie.rfc2109</code> </dt> <dd> +<p><code>True</code> if this cookie was received as an <span class="target" id="index-24"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2109.html"><strong>RFC 2109</strong></a> cookie (ie. the cookie arrived in a <em class="mailheader">Set-Cookie</em> header, and the value of the Version cookie-attribute in that header was 1). This attribute is provided because <a class="reference internal" href="#module-http.cookiejar" title="http.cookiejar: Classes for automatic handling of HTTP cookies."><code>http.cookiejar</code></a> may ‘downgrade’ RFC 2109 cookies to Netscape cookies, in which case <a class="reference internal" href="#http.cookiejar.Cookie.version" title="http.cookiejar.Cookie.version"><code>version</code></a> is 0.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.port_specified"> +<code>Cookie.port_specified</code> </dt> <dd> +<p><code>True</code> if a port or set of ports was explicitly specified by the server (in the <em class="mailheader">Set-Cookie</em> / <em class="mailheader">Set-Cookie2</em> header).</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.domain_specified"> +<code>Cookie.domain_specified</code> </dt> <dd> +<p><code>True</code> if a domain was explicitly specified by the server.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.domain_initial_dot"> +<code>Cookie.domain_initial_dot</code> </dt> <dd> +<p><code>True</code> if the domain explicitly specified by the server began with a dot (<code>'.'</code>).</p> </dd> +</dl> <p>Cookies may have additional non-standard cookie-attributes. These may be accessed using the following methods:</p> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.has_nonstandard_attr"> +<code>Cookie.has_nonstandard_attr(name)</code> </dt> <dd> +<p>Return <code>True</code> if cookie has the named cookie-attribute.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.get_nonstandard_attr"> +<code>Cookie.get_nonstandard_attr(name, default=None)</code> </dt> <dd> +<p>If cookie has the named cookie-attribute, return its value. Otherwise, return <em>default</em>.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.set_nonstandard_attr"> +<code>Cookie.set_nonstandard_attr(name, value)</code> </dt> <dd> +<p>Set the value of the named cookie-attribute.</p> </dd> +</dl> <p>The <a class="reference internal" href="#http.cookiejar.Cookie" title="http.cookiejar.Cookie"><code>Cookie</code></a> class also defines the following method:</p> <dl class="py method"> <dt class="sig sig-object py" id="http.cookiejar.Cookie.is_expired"> +<code>Cookie.is_expired(now=None)</code> </dt> <dd> +<p><code>True</code> if cookie has passed the time at which the server requested it should expire. If <em>now</em> is given (in seconds since the epoch), return whether the cookie has expired at the specified time.</p> </dd> +</dl> </section> <section id="examples"> <h2>Examples</h2> <p>The first example shows the most common usage of <a class="reference internal" href="#module-http.cookiejar" title="http.cookiejar: Classes for automatic handling of HTTP cookies."><code>http.cookiejar</code></a>:</p> <pre data-language="python">import http.cookiejar, urllib.request +cj = http.cookiejar.CookieJar() +opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) +r = opener.open("http://example.com/") +</pre> <p>This example illustrates how to open a URL using your Netscape, Mozilla, or Lynx cookies (assumes Unix/Netscape convention for location of the cookies file):</p> <pre data-language="python">import os, http.cookiejar, urllib.request +cj = http.cookiejar.MozillaCookieJar() +cj.load(os.path.join(os.path.expanduser("~"), ".netscape", "cookies.txt")) +opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) +r = opener.open("http://example.com/") +</pre> <p>The next example illustrates the use of <a class="reference internal" href="#http.cookiejar.DefaultCookiePolicy" title="http.cookiejar.DefaultCookiePolicy"><code>DefaultCookiePolicy</code></a>. Turn on <span class="target" id="index-25"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2965.html"><strong>RFC 2965</strong></a> cookies, be more strict about domains when setting and returning Netscape cookies, and block some domains from setting cookies or having them returned:</p> <pre data-language="python">import urllib.request +from http.cookiejar import CookieJar, DefaultCookiePolicy +policy = DefaultCookiePolicy( + rfc2965=True, strict_ns_domain=Policy.DomainStrict, + blocked_domains=["ads.net", ".ads.net"]) +cj = CookieJar(policy) +opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) +r = opener.open("http://example.com/") +</pre> </section> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/http.cookiejar.html" class="_attribution-link">https://docs.python.org/3.12/library/http.cookiejar.html</a> + </p> +</div> |
