python3從零學習-5.10.7、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

 

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>, ...]

 HTTP 狀態碼

已支持並且已在 http.HTTPStatus IANA 註冊 的狀態碼有:

雙字母代碼

映射名

詳情

100

CONTINUE:繼續

HTTP/1.1 RFC 7231, Section 6.2.1

101

SWITCHING_PROTOCOLS

HTTP/1.1 RFC 7231, Section 6.2.2

102

PROCESSING

WebDAV RFC 2518, Section 10.1

200

OK

HTTP/1.1 RFC 7231, Section 6.3.1

201

CREATED

HTTP/1.1 RFC 7231, Section 6.3.2

202

ACCEPTED

HTTP/1.1 RFC 7231, Section 6.3.3

203

NON_AUTHORITATIVE_INFORMATION

HTTP/1.1 RFC 7231, Section 6.3.4

204

NO_CONTENT: 沒有內容

HTTP/1.1 RFC 7231, Section 6.3.5

205

RESET_CONTENT

HTTP/1.1 RFC 7231, Section 6.3.6

206

PARTIAL_CONTENT

HTTP/1.1 RFC 7233, Section 4.1

207

MULTI_STATUS

WebDAV RFC 4918, Section 11.1

208

ALREADY_REPORTED

WebDAV Binding Extensions RFC 5842, Section 7.1 (Experimental)

226

IM_USED

Delta Encoding in HTTP RFC 3229, Section 10.4.1

300

MULTIPLE_CHOICES:有多種資源可選擇

HTTP/1.1 RFC 7231, Section 6.4.1

301

MOVED_PERMANENTLY:永久移動

HTTP/1.1 RFC 7231, Section 6.4.2

302

FOUND:臨時移動

HTTP/1.1 RFC 7231, Section 6.4.3

303

SEE_OTHER:已經移動

HTTP/1.1 RFC 7231, Section 6.4.4

304

NOT_MODIFIED:沒有修改

HTTP/1.1 RFC 7232, Section 4.1

305

USE_PROXY:使用代理

HTTP/1.1 RFC 7231, Section 6.4.5

307

TEMPORARY_REDIRECT:臨時重定向

HTTP/1.1 RFC 7231, Section 6.4.7

308

PERMANENT_REDIRECT:永久重定向

Permanent Redirect RFC 7238, Section 3 (Experimental)

400

BAD_REQUEST:錯誤請求

HTTP/1.1 RFC 7231, Section 6.5.1

401

UNAUTHORIZED:未授權

HTTP/1.1 Authentication RFC 7235, Section 3.1

402

PAYMENT_REQUIRED:保留,將來使用

HTTP/1.1 RFC 7231, Section 6.5.2

403

FORBIDDEN:禁止

HTTP/1.1 RFC 7231, Section 6.5.3

404

NOT_FOUND:沒有找到

HTTP/1.1 RFC 7231, Section 6.5.4

405

METHOD_NOT_ALLOWED:該請求方法不允許

HTTP/1.1 RFC 7231, Section 6.5.5

406

NOT_ACCEPTABLE:不可接受

HTTP/1.1 RFC 7231, Section 6.5.6

407

PROXY_AUTHENTICATION_REQUIRED:要求使用代理驗明正身

HTTP/1.1 Authentication RFC 7235, Section 3.2

408

REQUEST_TIMEOUT:請求超時

HTTP/1.1 RFC 7231, Section 6.5.7

409

CONFLICT:衝突

HTTP/1.1 RFC 7231, Section 6.5.8

410

GONE:已經不在了

HTTP/1.1 RFC 7231, Section 6.5.9

411

LENGTH_REQUIRED:長度要求

HTTP/1.1 RFC 7231, Section 6.5.10

412

PRECONDITION_FAILED:前提條件錯誤

HTTP/1.1 RFC 7232, Section 4.2

413

REQUEST_ENTITY_TOO_LARGE:請求體太大了

HTTP/1.1 RFC 7231, Section 6.5.11

414

REQUEST_URI_TOO_LONG:請求URI太長了

HTTP/1.1 RFC 7231, Section 6.5.12

415

UNSUPPORTED_MEDIA_TYPE:不支持的媒體格式

HTTP/1.1 RFC 7231, Section 6.5.13

416

REQUEST_RANGE_NOT_SATISFIABLE

HTTP/1.1 Range Requests RFC 7233, Section 4.4

417

EXPECTATION_FAILED:期望失敗

HTTP/1.1 RFC 7231, Section 6.5.14

422

UNPROCESSABLE_ENTITY:可加工實體

WebDAV RFC 4918, Section 11.2

423

LOCKED:鎖着

WebDAV RFC 4918, Section 11.3

424

FAILED_DEPENDENCY:失敗的依賴

WebDAV RFC 4918, Section 11.4

426

UPGRADE_REQUIRED:升級需要

HTTP/1.1 RFC 7231, Section 6.5.15

428

PRECONDITION_REQUIRED:先決條件要求

Additional HTTP Status Codes RFC 6585

429

TOO_MANY_REQUESTS:太多的請求

Additional HTTP Status Codes RFC 6585

431

REQUEST_HEADER_FIELDS_TOO_LARGE:請求頭太大

Additional HTTP Status Codes RFC 6585

500

INTERNAL_SERVER_ERROR:內部服務錯誤

HTTP/1.1 RFC 7231, Section 6.6.1

501

NOT_IMPLEMENTED:不可執行

HTTP/1.1 RFC 7231, Section 6.6.2

502

BAD_GATEWAY:無效網關

HTTP/1.1 RFC 7231, Section 6.6.3

503

SERVICE_UNAVAILABLE:服務不可用

HTTP/1.1 RFC 7231, Section 6.6.4

504

GATEWAY_TIMEOUT:網關超時

HTTP/1.1 RFC 7231, Section 6.6.5

505

HTTP_VERSION_NOT_SUPPORTED:HTTP版本不支持

HTTP/1.1 RFC 7231, Section 6.6.6

506

VARIANT_ALSO_NEGOTIATES:服務器存在內部配置錯誤

透明內容協商在: HTTP RFC 2295, Section 8.1 (實驗性的)

507

INSUFFICIENT_STORAGE:存儲不足

WebDAV RFC 4918, Section 11.5

508

LOOP_DETECTED:循環檢測

WebDAV Binding Extensions RFC 5842, Section 7.2 (Experimental)

510

NOT_EXTENDED:不擴展

An HTTP Extension Framework RFC 2774, Section 7 (Experimental)

511

NETWORK_AUTHENTICATION_REQUIRED:要求網絡身份驗證

Additional HTTP Status Codes RFC 6585, Section 6

爲了保持向後兼容性,枚舉值也以常量形式出現在 http.client 模塊中,。 枚舉名等於常量名 (例如 http.HTTPStatus.OK 也可以是 http.client.OK)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章