http —- HTTP 模块
源代码: Lib/http/__init__.py
http 是一个包,它收集了多个用于处理超文本传输协议的模块:
http.client 是一个低层级的 HTTP 协议客户端;对于高层级的 URL 访问请使用 urllib.request
http.server 包含基于 socketserver 的基本 HTTP 服务类
http.cookies 包含一些有用来实现通过 cookies 进行状态管理的工具
http.cookiejar 提供了 cookies 的持久化
The http module also defines the following enums that help you work with http related code:
class http.HTTPStatus
3.5 新版功能.
enum.IntEnum 的子类,它定义了组 HTTP 状态码,原理短语以及用英语书写的长描述文本。
用法:
>>> from http import HTTPStatus
>>> HTTPStatus.OK
HTTPStatus.OK
>>> HTTPStatus.OK == 200
True
>>> HTTPStatus.OK.value
200
>>> HTTPStatus.OK.phrase
'OK'
>>> HTTPStatus.OK.description
'Request fulfilled, document follows'
>>> list(HTTPStatus)
[HTTPStatus.CONTINUE, HTTPStatus.SWITCHING_PROTOCOLS, ...]
HTTP 状态码
Supported, IANA-registered status codes available in http.HTTPStatus are:
双字母代码 | 映射名 | 详情 |
---|---|---|
|
| HTTP/1.1 RFC 7231, 6.2.1 节 |
|
| HTTP/1.1 RFC 7231, 6.2.2 节 |
|
| WebDAV RFC 2518, 10.1 节 |
|
| 用于指定提示 RFC 8297 的 HTTP 状态码 |
|
| HTTP/1.1 RFC 7231, 6.3.1 节 |
|
| HTTP/1.1 RFC 7231, 6.3.2 节 |
|
| HTTP/1.1 RFC 7231, 6.3.3 节 |
|
| HTTP/1.1 RFC 7231, 6.3.4 节 |
|
| HTTP/1.1 RFC 7231, 6.3.5 节 |
|
| HTTP/1.1 RFC 7231, 6.3.6 节 |
|
| HTTP/1.1 RFC 7233, 4.1 节 |
|
| WebDAV RFC 4918, 11.1 节 |
|
| WebDAV Binding Extensions RFC 5842, 7.1 节(实验性) |
|
| Delta Encoding in HTTP RFC 3229, 10.4.1 节 |
|
| HTTP/1.1 RFC 7231, 6.4.1 节 |
|
| HTTP/1.1 RFC 7231, 6.4.2 节 |
|
| HTTP/1.1 RFC 7231, 6.4.3 节 |
|
| HTTP/1.1 RFC 7231, 6.4.4 节 |
|
| HTTP/1.1 RFC 7232, 4.1 节 |
|
| HTTP/1.1 RFC 7231, 6.4.5 节 |
|
| HTTP/1.1 RFC 7231, 6.4.7 节 |
|
| Permanent Redirect RFC 7238, Section 3 (Experimental) |
|
| HTTP/1.1 RFC 7231, 6.5.1 节 |
|
| HTTP/1.1 Authentication RFC 7235, 3.1 节 |
|
| HTTP/1.1 RFC 7231, 6.5.2 节 |
|
| HTTP/1.1 RFC 7231, 6.5.3 节 |
|
| HTTP/1.1 RFC 7231, 6.5.4 节 |
|
| HTTP/1.1 RFC 7231, 6.5.5 节 |
|
| HTTP/1.1 RFC 7231, 6.5.6 节 |
|
| HTTP/1.1 Authentication RFC 7235, 3.1 节 |
|
| HTTP/1.1 RFC 7231, 6.5.7 节 |
|
| HTTP/1.1 RFC 7231, 6.5.8 节 |
|
| HTTP/1.1 RFC 7231, 6.5.9 节 |
|
| HTTP/1.1 RFC 7231, 6.5.10 节 |
|
| HTTP/1.1 RFC 7232, 4.2 节 |
|
| HTTP/1.1 RFC 7231, 6.5.11 节 |
|
| HTTP/1.1 RFC 7231, 6.5.12 节 |
|
| HTTP/1.1 RFC 7231, 6.5.13 节 |
|
| HTTP/1.1 Range Requests RFC 7233, 4.4 节 |
|
| HTTP/1.1 RFC 7231, 6.5.14 节 |
|
| HTCPCP/1.0 RFC 2324, Section 2.3.2 |
|
| HTTP/2 RFC 7540, 9.1.2 节 |
|
| WebDAV RFC 4918, 11.2 节 |
|
| WebDAV RFC 4918, 11.3 节 |
|
| WebDAV RFC 4918, 11.4 节 |
|
| 使用 HTTP RFC 8470 中的早期数据 |
|
| HTTP/1.1 RFC 7231, 6.5.15 节 |
|
| Additional HTTP Status Codes RFC 6585 |
|
| Additional HTTP Status Codes RFC 6585 |
|
| Additional HTTP Status Codes RFC 6585 |
|
| HTTP 状态码用于报告法律障碍 RFC 7725 |
|
| HTTP/1.1 RFC 7231, 6.6.1 节 |
|
| HTTP/1.1 RFC 7231, 6.6.2 节 |
|
| HTTP/1.1 RFC 7231, 6.6.3 节 |
|
| HTTP/1.1 RFC 7231, 6.6.4 节 |
|
| HTTP/1.1 RFC 7231, 6.6.5 节 |
|
| HTTP/1.1 RFC 7231, 6.6.6 节 |
|
| 透明内容协商在: HTTP RFC 2295, 8.1 节(实验性) |
|
| WebDAV RFC 4918, 11.5 节 |
|
| WebDAV Binding Extensions RFC 5842, 7.2 节(实验性) |
|
| WebDAV Binding Extensions RFC 5842, 7.2 节(实验性) |
|
| Additional HTTP Status Codes RFC 6585, 6 节 |
为了保持向后兼容性,枚举值也以常量形式出现在 http.client 模块中,。 枚举名等于常量名 (例如 http.HTTPStatus.OK
也可以是 http.client.OK
)。
在 3.7 版更改: 添加了 421 MISDIRECTED_REQUEST
状态码。
3.8 新版功能: 添加了 451 UNAVAILABLE_FOR_LEGAL_REASONS
状态码。
3.9 新版功能: 增加了 103 EARLY_HINTS
, 418 IM_A_TEAPOT
和 425 TOO_EARLY
状态码
class http.HTTPMethod
3.11 新版功能.
A subclass of enum.StrEnum that defines a set of HTTP methods and descriptions written in English.
用法:
>>> from http import HTTPMethod
>>> HTTMethod.GET
HTTMethod.GET
>>> HTTMethod.GET == 'GET'
True
>>> HTTMethod.GET.value
'GET'
>>> HTTMethod.GET.description
'Transfer a current representation of the target resource.'
>>> list(HTTPMethod)
[HTTPMethod.GET, HTTPMethod.HEAD, ...]
HTTP methods
Supported, IANA-registered methods available in http.HTTPMethod are:
Method | 映射名 | 详情 |
---|---|---|
|
| HTTP/1.1 RFC 7231, Section 4.3.1 |
|
| HTTP/1.1 RFC 7231, Section 4.3.2 |
|
| HTTP/1.1 RFC 7231, Section 4.3.3 |
|
| HTTP/1.1 RFC 7231, Section 4.3.4 |
|
| HTTP/1.1 RFC 7231, Section 4.3.5 |
|
| HTTP/1.1 RFC 7231, Section 4.3.6 |
|
| HTTP/1.1 RFC 7231, Section 4.3.7 |
|
| HTTP/1.1 RFC 7231, Section 4.3.8 |
|
| HTTP/1.1 RFC 5789 |