http
--- HTTP 模块¶
源代码: Lib/http/__init__.py
http
是一个包,它收集了多个用于处理超文本传输协议的模块:
http.client
是一个底层的 HTTP 协议客户端;对于高层级的 URL 访问请使用urllib.request
http.server
包含基于socketserver
的基本 HTTP 服务类http.cookies
包含一些有用来实现通过 cookies 进行状态管理的工具http.cookiejar
提供了 cookies 的持久化
http
模块还定义了下列枚举来帮助你使用 http 相关的代码:
- class http.HTTPStatus¶
Added in version 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 状态码¶
已支持的,IANA 注册的状态码 在 http.HTTPStatus
中可用的有:
双字母代码 |
映射名 |
详情 |
---|---|---|
|
|
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
状态码。
Added in version 3.8: 添加了 451 UNAVAILABLE_FOR_LEGAL_REASONS
状态码。
Added in version 3.9: 增加了 103 EARLY_HINTS
, 418 IM_A_TEAPOT
和 425 TOO_EARLY
状态码
HTTP 状态类别¶
Added in version 3.12.
这些枚举值具有一些用于指明 HTTP 状态类别的特征属性:
特征属性 |
表示 |
详情 |
---|---|---|
|
|
HTTP/1.1 RFC 7231, Section 6 |
|
|
HTTP/1.1 RFC 7231, Section 6 |
|
|
HTTP/1.1 RFC 7231, Section 6 |
|
|
HTTP/1.1 RFC 7231, Section 6 |
|
|
HTTP/1.1 RFC 7231, Section 6 |
用法:
>>> from http import HTTPStatus >>> HTTPStatus.OK.is_success True >>> HTTPStatus.OK.is_client_error False
- class http.HTTPMethod¶
Added in version 3.11.
一个
enum.StrEnum
的子类,它定义了一组 HTTP 方法以及用英文书写的描述。用法:
>>> from http import HTTPMethod >>> >>> HTTPMethod.GET <HTTPMethod.GET> >>> HTTPMethod.GET == 'GET' True >>> HTTPMethod.GET.value 'GET' >>> HTTPMethod.GET.description 'Retrieve the target.' >>> list(HTTPMethod) [<HTTPMethod.CONNECT>, <HTTPMethod.DELETE>, <HTTPMethod.GET>, <HTTPMethod.HEAD>, <HTTPMethod.OPTIONS>, <HTTPMethod.PATCH>, <HTTPMethod.POST>, <HTTPMethod.PUT>, <HTTPMethod.TRACE>]
HTTP 方法¶
已支持的,IANA 注册的方法 在 http.HTTPMethod
中可用的有:
方法 |
映射名 |
详情 |
---|---|---|
|
|
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 |