http.cookiejar — 用于 HTTP 客户端的 Cookie 处理

源代码: Lib/http/cookiejar.py


The http.cookiejar module defines classes for automatic handling of HTTP cookies. It is useful for accessing web sites that require small pieces of data – Cookie – 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 RFC 2965 are handled. RFC 2965 handling is switched off by default. RFC 2109 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. http.cookiejar 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 max-age and port cookie-attributes introduced with RFC 2965.

注意

The various named parameters found in Set-Cookie and Set-Cookie2 headers (eg. domain and expires ) are conventionally referred to as 属性 . To distinguish them from Python attributes, the documentation for this module uses the term cookie-attribute 代替。

模块定义以下异常:

exception http.cookiejar. LoadError

实例化的 FileCookieJar raise this exception on failure to load cookies from a file. LoadError 是子类化的 OSError .

3.3 版改变: LoadError used to be a subtype of IOError ,现在是别名化的 OSError .

提供了下列类:

class http.cookiejar. CookieJar ( policy = None )

policy 是对象实现 CookiePolicy 接口。

The CookieJar class stores HTTP cookies. It extracts cookies from HTTP requests, and returns them in HTTP responses. CookieJar instances automatically expire contained cookies when necessary. Subclasses are also responsible for storing and retrieving cookies from a file or database.

class http.cookiejar. FileCookieJar ( filename = None , delayload = None , policy = None )

policy 是对象实现 CookiePolicy interface. For the other arguments, see the documentation for the corresponding attributes.

A CookieJar which can load cookies from, and perhaps save cookies to, a file on disk. Cookies are NOT loaded from the named file until either the load() or revert() method is called. Subclasses of this class are documented in section FileCookieJar 子类和与 Web 浏览器合作 .

This should not be initialized directly – use its subclasses below instead.

3.8 版改变: 文件名参数支持 像路径对象 .