http --- HTTP 模組¶
http 是一個收集了多個用於處理超文本傳輸協定 (HyperText Transfer Protocol) 之模組 (module) 的套件:
http.client是一個低階的 HTTP 協定用戶端;對於高階的 URL 存取請使用urllib.requesthttp.server包含基於socketserver的基本 HTTP 伺服器類別http.cookies包含通過 cookies 實作狀態管理的工具程式 (utilities)http.cookiejar提供了 cookies 的持續留存 (persistence)
Thehttp module also defines the following enums that help you work with http related code:
- classhttp.HTTPStatus¶
在 3.5 版被加入.
enum.IntEnum的子類別,它定義了一組 HTTP 狀態碼、原理短語 (reason phrase) 以及英文長描述。用法:
>>>fromhttpimportHTTPStatus>>>HTTPStatus.OKHTTPStatus.OK>>>HTTPStatus.OK==200True>>>HTTPStatus.OK.value200>>>HTTPStatus.OK.phrase'OK'>>>HTTPStatus.OK.description'Request fulfilled, document follows'>>>list(HTTPStatus)[HTTPStatus.CONTINUE, HTTPStatus.SWITCHING_PROTOCOLS, ...]
HTTP 狀態碼¶
http.HTTPStatus 當中,已支援並且有於IANA 註冊的狀態碼有:
狀態碼 | 列舉名稱 | 詳情 |
|---|---|---|
|
| HTTP SemanticsRFC 9110, Section 15.2.1 |
|
| HTTP SemanticsRFC 9110, Section 15.2.2 |
|
| WebDAVRFC 2518,10.1 節 |
|
| 用於指定提示 (Indicating Hints)RFC 8297 的 HTTP 狀態碼 |
|
| HTTP SemanticsRFC 9110, Section 15.3.1 |
|
| HTTP SemanticsRFC 9110, Section 15.3.2 |
|
| HTTP SemanticsRFC 9110, Section 15.3.3 |
|
| HTTP SemanticsRFC 9110, Section 15.3.4 |
|
| HTTP SemanticsRFC 9110, Section 15.3.5 |
|
| HTTP SemanticsRFC 9110, Section 15.3.6 |
|
| HTTP SemanticsRFC 9110, Section 15.3.7 |
|
| WebDAVRFC 4918,11.1 節 |
|
| WebDAV 繫結擴充 (Binding Extensions)RFC 5842,7.1 節(實驗性) |
|
| HTTP 中的差分編碼RFC 3229,10.4.1 節 |
|
| HTTP SemanticsRFC 9110, Section 15.4.1 |
|
| HTTP SemanticsRFC 9110, Section 15.4.2 |
|
| HTTP SemanticsRFC 9110, Section 15.4.3 |
|
| HTTP SemanticsRFC 9110, Section 15.4.4 |
|
| HTTP SemanticsRFC 9110, Section 15.4.5 |
|
| HTTP SemanticsRFC 9110, Section 15.4.6 |
|
| HTTP SemanticsRFC 9110, Section 15.4.8 |
|
| HTTP SemanticsRFC 9110, Section 15.4.9 |
|
| HTTP SemanticsRFC 9110, Section 15.5.1 |
|
| HTTP SemanticsRFC 9110, Section 15.5.2 |
|
| HTTP SemanticsRFC 9110, Section 15.5.3 |
|
| HTTP SemanticsRFC 9110, Section 15.5.4 |
|
| HTTP SemanticsRFC 9110, Section 15.5.5 |
|
| HTTP SemanticsRFC 9110, Section 15.5.6 |
|
| HTTP SemanticsRFC 9110, Section 15.5.7 |
|
| HTTP SemanticsRFC 9110, Section 15.5.8 |
|
| HTTP SemanticsRFC 9110, Section 15.5.9 |
|
| HTTP SemanticsRFC 9110, Section 15.5.10 |
|
| HTTP SemanticsRFC 9110, Section 15.5.11 |
|
| HTTP SemanticsRFC 9110, Section 15.5.12 |
|
| HTTP SemanticsRFC 9110, Section 15.5.13 |
|
| HTTP SemanticsRFC 9110, Section 15.5.14 |
|
| HTTP SemanticsRFC 9110, Section 15.5.15 |
|
| HTTP SemanticsRFC 9110, Section 15.5.16 |
|
| HTTP SemanticsRFC 9110, Section 15.5.17 |
|
| HTTP SemanticsRFC 9110, Section 15.5.18 |
|
| HTCPCP/1.0RFC 2324,Section 2.3.2 |
|
| HTTP SemanticsRFC 9110, Section 15.5.20 |
|
| HTTP SemanticsRFC 9110, Section 15.5.21 |
|
| WebDAVRFC 4918,11.3 節 |
|
| WebDAVRFC 4918,11.4 節 |
|
| 使用 HTTP 中的早期資料RFC 8470 |
|
| HTTP SemanticsRFC 9110, Section 15.5.22 |
|
| 額外的 HTTP 狀態碼RFC 6585 |
|
| 額外的 HTTP 狀態碼RFC 6585 |
|
| 額外的 HTTP 狀態碼RFC 6585 |
|
| 一個用來回報合法性障礙 (Legal Obstacles) 的 HTTP 狀態碼RFC 7725 |
|
| HTTP SemanticsRFC 9110, Section 15.6.1 |
|
| HTTP SemanticsRFC 9110, Section 15.6.2 |
|
| HTTP SemanticsRFC 9110, Section 15.6.3 |
|
| HTTP SemanticsRFC 9110, Section 15.6.4 |
|
| HTTP SemanticsRFC 9110, Section 15.6.5 |
|
| HTTP SemanticsRFC 9110, Section 15.6.6 |
|
| HTTP 中的透明內容協商 (Transparent Content Negotiation)RFC 2295,8.1 節(實驗性) |
|
| WebDAVRFC 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 版的變更:新增421MISDIRECTED_REQUEST 狀態碼。
在 3.8 版被加入:新增451UNAVAILABLE_FOR_LEGAL_REASONS 狀態碼。
在 3.9 版被加入:新增103EARLY_HINTS、418IM_A_TEAPOT 與425TOO_EARLY 狀態碼。
在 3.13 版的變更:Implemented RFC9110 naming for status constants. Old constant names are preserved forbackwards compatibility:413REQUEST_ENTITY_TOO_LARGE,414REQUEST_URI_TOO_LONG,416REQUESTED_RANGE_NOT_SATISFIABLE and422UNPROCESSABLE_ENTITY.
HTTP 狀態分類¶
在 3.12 版被加入.
The enum values have several properties to indicate the HTTP status category:
Property | Indicates that | 詳情 |
|---|---|---|
|
| HTTP SemanticsRFC 9110, Section 15 |
|
| HTTP SemanticsRFC 9110, Section 15 |
|
| HTTP SemanticsRFC 9110, Section 15 |
|
| HTTP SemanticsRFC 9110, Section 15 |
|
| HTTP SemanticsRFC 9110, Section 15 |
用法:
>>>fromhttpimportHTTPStatus>>>HTTPStatus.OK.is_successTrue>>>HTTPStatus.OK.is_client_errorFalse
- classhttp.HTTPMethod¶
在 3.11 版被加入.
enum.StrEnum的子類別,它定義了一組 HTTP 方法以及英文描述。用法:
>>>fromhttpimportHTTPMethod>>>>>>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 SemanticsRFC 9110, Section 9.3.1 |
|
| HTTP SemanticsRFC 9110, Section 9.3.2 |
|
| HTTP SemanticsRFC 9110, Section 9.3.3 |
|
| HTTP SemanticsRFC 9110, Section 9.3.4 |
|
| HTTP SemanticsRFC 9110, Section 9.3.5 |
|
| HTTP SemanticsRFC 9110, Section 9.3.6 |
|
| HTTP SemanticsRFC 9110, Section 9.3.7 |
|
| HTTP SemanticsRFC 9110, Section 9.3.8 |
|
| HTTP/1.1RFC 5789 |