21.11. 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.HTTPStatus 枚举定义了一些 HTTP 状态码以及相关联消息的模块
class http.HTTPStatus
3.5 新版功能.
enum.IntEnum 的子类,它定义了组 HTTP 状态码,原理短语以及用英语书写的长描述文本。
用法:
>>> from http import HTTPStatus
>>> HTTPStatus.OK
<HTTPStatus.OK: 200>
>>> HTTPStatus.OK == 200
True
>>> http.HTTPStatus.OK.value
200
>>> HTTPStatus.OK.phrase
'OK'
>>> HTTPStatus.OK.description
'Request fulfilled, document follows'
>>> list(HTTPStatus)
[<HTTPStatus.CONTINUE: 100>, <HTTPStatus.SWITCHING_PROTOCOLS: 101>, ...]
21.11.1. HTTP 状态码
已支持并且已在 http.HTTPStatus IANA 注册 的状态码有:
双字母代码 | 映射名 | 详情 |
---|---|---|
|
| HTTP/1.1 RFC 7231, Section 6.2.1 |
|
| HTTP/1.1 RFC 7231, Section 6.2.2 |
|
| WebDAV RFC 2518, Section 10.1 |
|
| HTTP/1.1 RFC 7231, Section 6.3.1 |
|
| HTTP/1.1 RFC 7231, Section 6.3.2 |
|
| HTTP/1.1 RFC 7231, Section 6.3.3 |
|
| HTTP/1.1 RFC 7231, Section 6.3.4 |
|
| HTTP/1.1 RFC 7231, Section 6.3.5 |
|
| HTTP/1.1 RFC 7231, Section 6.3.6 |
|
| HTTP/1.1 RFC 7233, Section 4.1 |
|
| WebDAV RFC 4918, Section 11.1 |
|
| WebDAV Binding Extensions RFC 5842, Section 7.1 (Experimental) |
|
| Delta Encoding in HTTP RFC 3229, Section 10.4.1 |
|
| HTTP/1.1 RFC 7231, Section 6.4.1 |
|
| HTTP/1.1 RFC 7231, Section 6.4.2 |
|
| HTTP/1.1 RFC 7231, Section 6.4.3 |
|
| HTTP/1.1 RFC 7231, Section 6.4.4 |
|
| HTTP/1.1 RFC 7232, Section 4.1 |
|
| HTTP/1.1 RFC 7231, Section 6.4.5 |
|
| HTTP/1.1 RFC 7231, Section 6.4.7 |
|
| Permanent Redirect RFC 7238, Section 3 (Experimental) |
|
| HTTP/1.1 RFC 7231, Section 6.5.1 |
|
| HTTP/1.1 Authentication RFC 7235, Section 3.1 |
|
| HTTP/1.1 RFC 7231, Section 6.5.2 |
|
| HTTP/1.1 RFC 7231, Section 6.5.3 |
|
| HTTP/1.1 RFC 7231, Section 6.5.4 |
|
| HTTP/1.1 RFC 7231, Section 6.5.5 |
|
| HTTP/1.1 RFC 7231, Section 6.5.6 |
|
| HTTP/1.1 Authentication RFC 7235, Section 3.2 |
|
| HTTP/1.1 RFC 7231, Section 6.5.7 |
|
| HTTP/1.1 RFC 7231, Section 6.5.8 |
|
| HTTP/1.1 RFC 7231, Section 6.5.9 |
|
| HTTP/1.1 RFC 7231, Section 6.5.10 |
|
| HTTP/1.1 RFC 7232, Section 4.2 |
|
| HTTP/1.1 RFC 7231, Section 6.5.11 |
|
| HTTP/1.1 RFC 7231, Section 6.5.12 |
|
| HTTP/1.1 RFC 7231, Section 6.5.13 |
|
| HTTP/1.1 Range Requests RFC 7233, Section 4.4 |
|
| HTTP/1.1 RFC 7231, Section 6.5.14 |
|
| WebDAV RFC 4918, Section 11.2 |
|
| WebDAV RFC 4918, Section 11.3 |
|
| WebDAV RFC 4918, Section 11.4 |
|
| HTTP/1.1 RFC 7231, Section 6.5.15 |
|
| Additional HTTP Status Codes RFC 6585 |
|
| Additional HTTP Status Codes RFC 6585 |
|
| Additional HTTP Status Codes RFC 6585 |
|
| HTTP/1.1 RFC 7231, Section 6.6.1 |
|
| HTTP/1.1 RFC 7231, Section 6.6.2 |
|
| HTTP/1.1 RFC 7231, Section 6.6.3 |
|
| HTTP/1.1 RFC 7231, Section 6.6.4 |
|
| HTTP/1.1 RFC 7231, Section 6.6.5 |
|
| HTTP/1.1 RFC 7231, Section 6.6.6 |
|
| 透明内容协商在: HTTP RFC 2295, Section 8.1 (实验性的) |
|
| WebDAV RFC 4918, Section 11.5 |
|
| WebDAV Binding Extensions RFC 5842, Section 7.2 (Experimental) |
|
| An HTTP Extension Framework RFC 2774, Section 7 (Experimental) |
|
| Additional HTTP Status Codes RFC 6585, Section 6 |
为了保持向后兼容性,枚举值也以常量形式出现在 http.client 模块中,。 枚举名等于常量名 (例如 http.HTTPStatus.OK
也可以是 http.client.OK
)。