http
--- HTTP 模組¶
http
是一個收集了多個用於處理超文本傳輸協定 (HyperText Transfer Protocol) 之模組 (module) 的套件:
http.client
是一個低階的 HTTP 協定客戶端;對於高階的 URL 訪問請使用urllib.request
http.server
包含基於socketserver
的基本 HTTP 伺服器類別http.cookies
包含通過 cookies 實作狀態管理的工具程式 (utilities)http.cookiejar
提供了 cookies 的持續留存 (persistence)
http
模块还定义了下列枚举来帮助你使用 http 相关的代码:
- class http.HTTPStatus¶
Added in version 3.5.
enum.IntEnum
的子類別,它定義了一組 HTTP 狀態碼、原理短語 (reason phrase) 以及英文長描述。用法:
>>> 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 狀態碼¶
http.HTTPStatus
當中,已支援並且有於 IANA 註冊的狀態碼有:
狀態碼 |
列舉名稱 |
詳情 |
---|---|---|
|
|
HTTP Semantics RFC 9110, Section 15.2.1 |
|
|
HTTP Semantics RFC 9110, Section 15.2.2 |
|
|
WebDAV RFC 2518,10.1 節 |
|
|
用於指定提示 (Indicating Hints) RFC 8297 的 HTTP 狀態碼 |
|
|
HTTP Semantics RFC 9110, Section 15.3.1 |
|
|
HTTP Semantics RFC 9110, Section 15.3.2 |
|
|
HTTP Semantics RFC 9110, Section 15.3.3 |
|
|
HTTP Semantics RFC 9110, Section 15.3.4 |
|
|
HTTP Semantics RFC 9110, Section 15.3.5 |
|
|
HTTP Semantics RFC 9110, Section 15.3.6 |
|
|
HTTP Semantics RFC 9110, Section 15.3.7 |
|
|
WebDAV RFC 4918,11.1 節 |
|
|
WebDAV 繫結擴充 (Binding Extensions) RFC 5842,7.1 節(實驗性) |
|
|
HTTP 中的差分編碼 RFC 3229,10.4.1 節 |
|
|
HTTP Semantics RFC 9110, Section 15.4.1 |
|
|
HTTP Semantics RFC 9110, Section 15.4.2 |
|
|
HTTP Semantics RFC 9110, Section 15.4.3 |
|
|
HTTP Semantics RFC 9110, Section 15.4.4 |
|
|
HTTP Semantics RFC 9110, Section 15.4.5 |
|
|
HTTP Semantics RFC 9110, Section 15.4.6 |
|
|
HTTP Semantics RFC 9110, Section 15.4.8 |
|
|
HTTP Semantics RFC 9110, Section 15.4.9 |
|
|
HTTP Semantics RFC 9110, Section 15.5.1 |
|
|
HTTP Semantics RFC 9110, Section 15.5.2 |
|
|
HTTP Semantics RFC 9110, Section 15.5.3 |
|
|
HTTP Semantics RFC 9110, Section 15.5.4 |
|
|
HTTP Semantics RFC 9110, Section 15.5.5 |
|
|
HTTP Semantics RFC 9110, Section 15.5.6 |
|
|
HTTP Semantics RFC 9110, Section 15.5.7 |
|
|
HTTP Semantics RFC 9110, Section 15.5.8 |
|
|
HTTP Semantics RFC 9110, Section 15.5.9 |
|
|
HTTP Semantics RFC 9110, Section 15.5.10 |
|
|
HTTP Semantics RFC 9110, Section 15.5.11 |
|
|
HTTP Semantics RFC 9110, Section 15.5.12 |
|
|
HTTP Semantics RFC 9110, Section 15.5.13 |
|
|
HTTP Semantics RFC 9110, Section 15.5.14 |
|
|
HTTP Semantics RFC 9110, Section 15.5.15 |
|
|
HTTP Semantics RFC 9110, Section 15.5.16 |
|
|
HTTP Semantics RFC 9110, Section 15.5.17 |
|
|
HTTP Semantics RFC 9110, Section 15.5.18 |
|
|
HTCPCP/1.0 RFC 2324,Section 2.3.2 |
|
|
HTTP Semantics RFC 9110, Section 15.5.20 |
|
|
HTTP Semantics RFC 9110, Section 15.5.21 |
|
|
WebDAV RFC 4918,11.3 節 |
|
|
WebDAV RFC 4918,11.4 節 |
|
|
使用 HTTP 中的早期資料 RFC 8470 |
|
|
HTTP Semantics RFC 9110, Section 15.5.22 |
|
|
額外的 HTTP 狀態碼 RFC 6585 |
|
|
額外的 HTTP 狀態碼 RFC 6585 |
|
|
額外的 HTTP 狀態碼 RFC 6585 |
|
|
一個用來回報合法性障礙 (Legal Obstacles) 的 HTTP 狀態碼 RFC 7725 |
|
|
HTTP Semantics RFC 9110, Section 15.6.1 |
|
|
HTTP Semantics RFC 9110, Section 15.6.2 |
|
|
HTTP Semantics RFC 9110, Section 15.6.3 |
|
|
HTTP Semantics RFC 9110, Section 15.6.4 |
|
|
HTTP Semantics RFC 9110, Section 15.6.5 |
|
|
HTTP Semantics RFC 9110, Section 15.6.6 |
|
|
HTTP 中的透明內容協商 (Transparent Content Negotiation) RFC 2295,8.1 節(實驗性) |
|
|
WebDAV RFC 4918,11.5 節 |
|
|
WebDAV 繫結擴充 RFC 5842,7.2 節(實驗性) |
|
|
一個 HTTP 擴充框架 RFC 2774,7 節(實驗性) |
|
|
額外的 HTTP 狀態碼 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
狀態碼。
在 3.13 版的變更: 实现了针对状态常量的 RFC9110 命名。 旧常量名被保留用于向下兼容。
HTTP 狀態分類¶
Added in version 3.12.
这些枚举值具有一些用于指明 HTTP 状态类别的特征属性:
特征属性 |
表示 |
詳情 |
---|---|---|
|
|
HTTP Semantics RFC 9110, Section 15 |
|
|
HTTP Semantics RFC 9110, Section 15 |
|
|
HTTP Semantics RFC 9110, Section 15 |
|
|
HTTP Semantics RFC 9110, Section 15 |
|
|
HTTP Semantics RFC 9110, Section 15 |
用法:
>>> 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 方法¶
http.HTTPStatus
當中,已支援並且有於 IANA 註冊的狀態碼有:
方法 |
列舉名稱 |
詳情 |
---|---|---|
|
|
HTTP Semantics RFC 9110, Section 9.3.1 |
|
|
HTTP Semantics RFC 9110, Section 9.3.2 |
|
|
HTTP Semantics RFC 9110, Section 9.3.3 |
|
|
HTTP Semantics RFC 9110, Section 9.3.4 |
|
|
HTTP Semantics RFC 9110, Section 9.3.5 |
|
|
HTTP Semantics RFC 9110, Section 9.3.6 |
|
|
HTTP Semantics RFC 9110, Section 9.3.7 |
|
|
HTTP Semantics RFC 9110, Section 9.3.8 |
|
|
HTTP/1.1 RFC 5789 |