http — HTTP modules

Source code: Lib/http/__init__.py


http is a package that collects several modules for working with the HyperText Transfer Protocol:是一个收集多个模块以使用超文本传输协议的包:

http is also a module that defines a number of HTTP status codes and associated messages through the http.HTTPStatus enum:也是一个通过http.HTTPStatus枚举定义多个HTTP状态代码和相关消息的模块:

classhttp.HTTPStatus

New in version 3.5.版本3.5中新增。

A subclass of enum.IntEnum that defines a set of HTTP status codes, reason phrases and long descriptions written in English.enum.IntEnum的一个子类,定义了一组HTTP状态代码、原因短语和用英语编写的长描述。

Usage:用法:

>>> from http import HTTPStatus
>>> HTTPStatus.OK
<HTTPStatus.OK: 200>
>>> HTTPStatus.OK == 200
True
>>> 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 status codes

Supported, IANA-registered status codes available in http.HTTPStatus are:支持,http.HTTPStatus中可用的IANA注册状态代码为:

Code

Enum Name

Details

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

103

EARLY_HINTS

An HTTP Status Code for Indicating Hints 用于指示提示的HTTP状态代码RFC 8297

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)WebDAV绑定扩展RFC 5842第7.1节(实验)

226

IM_USED

Delta Encoding in HTTP RFC 3229, Section 10.4.1HTTP RFC 3229第10.4.1节中的Delta编码

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

HTTP/1.1 RFC 7231, Section 6.5.12

415

UNSUPPORTED_MEDIA_TYPE

HTTP/1.1 RFC 7231, Section 6.5.13

416

REQUESTED_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

418

IM_A_TEAPOT

HTCPCP/1.0 RFC 2324, Section 2.3.2

421

MISDIRECTED_REQUEST

HTTP/2 RFC 7540, Section 9.1.2

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

425

TOO_EARLY

Using Early Data in HTTP RFC 8470

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

451

UNAVAILABLE_FOR_LEGAL_REASONS

An HTTP Status Code to Report Legal Obstacles RFC 7725

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/1.1 RFC 7231, Section 6.6.6

506

VARIANT_ALSO_NEGOTIATES

Transparent Content Negotiation in HTTP RFC 2295, Section 8.1 (Experimental)HTTP RFC 2295第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状态代码RFC 6585,第6节

In order to preserve backwards compatibility, enum values are also present in the http.client module in the form of constants. 为了保持向后兼容性,http.client模块中还以常量的形式存在枚举值。The enum name is equal to the constant name (i.e. http.HTTPStatus.OK is also available as http.client.OK).枚举名称等于常量名称(即http.HTTPStatus.OK也可用作http.client.OK)。

Changed in version 3.7:版本3.7中更改: Added 421 MISDIRECTED_REQUEST status code.添加421 MISDIRECTED_REQUEST状态代码。

New in version 3.8.版本3.8中新增。Added 451 UNAVAILABLE_FOR_LEGAL_REASONS status code.添加451 UNAVAILABLE_FOR_LEGAL_REASONS状态代码。

New in version 3.9.版本3.9中新增。Added 103 EARLY_HINTS, 418 IM_A_TEAPOT and 425 TOO_EARLY status codes.增加了103 EARLY_HINTS418 IM_A_TEAPOT425 TOO_EARLY状态代码。