http.cookiejar— 用于 HTTP 客户端的 Cookie 处理 ¶
module defines classes for automatic handling of HTTP cookies. It is useful for accessing web sites that require small pieces of data –
– 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.
Both the regular Netscape cookie protocol and the protocol defined by
are handled. RFC 2965 handling is switched off by default.
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.
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
cookie-attributes introduced with RFC 2965.
The various named parameters found in
) are conventionally referred to as
. To distinguish them from Python attributes, the documentation for this module uses the term
class stores HTTP cookies. It extracts cookies from HTTP requests, and returns them in HTTP responses.
instances automatically expire contained cookies when necessary. Subclasses are also responsible for storing and retrieving cookies from a file or database.
interface. For the other arguments, see the documentation for the corresponding attributes.
which can load cookies from, and perhaps save cookies to, a file on disk. Cookies are
loaded from the named file until either the
method is called. Subclasses of this class are documented in section
FileCookieJar 子类和与 Web 浏览器合作
3.8 版改变： 文件名参数支持 像路径对象 .
This class is responsible for deciding whether each cookie should be accepted from / returned to the server.
Constructor arguments should be passed as keyword arguments only.
is a sequence of domain names that we never accept cookies from, nor return cookies to.
, this is a sequence of the only domains for which we accept and return cookies.
is a sequence of protocols for which secure cookies can be added to. By default
(secure websocket) are considered secure protocols. For all other arguments, see the documentation for
implements the standard accept / reject rules for Netscape and
cookies. By default,
cookies (ie. cookies received in a
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
, RFC 2109 cookies are ‘downgraded’ by the
instance to Netscape cookies, by setting the
instance to 0.
also provides some parameters to allow some fine-tuning of policy.
采用自动 Cookie 处理打开 URL。
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
) only bears a passing resemblance to the one sketched out in
Obsoleted by RFC 2965 . Uses Set-Cookie with version=1.
The Netscape protocol with the bugs fixed. Uses Set-Cookie2 in place of Set-Cookie . Not widely used.
Unfinished errata to RFC 2965 .
RFC 2964 - Use of HTTP State Management
The first example shows the most common usage of
import http.cookiejar, urllib.request cj = http.cookiejar.CookieJar() opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) r = opener.open("http://example.com/")
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):
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/")
The next example illustrates the use of
. Turn on
cookies, be more strict about domains when setting and returning Netscape cookies, and block some domains from setting cookies or having them returned:
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/")