http —- HTTP 模块

源代码:Lib/http/init.py


http 是一个包,它收集了多个用于处理超文本传输协议的模块:

http 也是一个通过 http.HTTPStatus 枚举定义了一些 HTTP 状态码以及相关联消息的模块

  • class http.HTTPStatus

3.5 新版功能.

enum.IntEnum 的子类,它定义了组 HTTP 状态码,原理短语以及用英语书写的长描述文本。

用法:

  1. >>> from http import HTTPStatus
  2. >>> HTTPStatus.OK
  3. <HTTPStatus.OK: 200>
  4. >>> HTTPStatus.OK == 200
  5. True
  6. >>> HTTPStatus.OK.value
  7. 200
  8. >>> HTTPStatus.OK.phrase
  9. 'OK'
  10. >>> HTTPStatus.OK.description
  11. 'Request fulfilled, document follows'
  12. >>> list(HTTPStatus)
  13. [<HTTPStatus.CONTINUE: 100>, <HTTPStatus.SWITCHING_PROTOCOLS: 101>, ...]

HTTP 状态码

已支持并且已在 http.HTTPStatusIANA 注册 的状态码有:

双字母代码映射名详情
100CONTINUE:继续HTTP/1.1 RFC 7231, Section 6.2.1
101SWITCHING_PROTOCOLSHTTP/1.1 RFC 7231, Section 6.2.2
102PROCESSINGWebDAV RFC 2518, Section 10.1
200OKHTTP/1.1 RFC 7231, Section 6.3.1
201CREATEDHTTP/1.1 RFC 7231, Section 6.3.2
202ACCEPTEDHTTP/1.1 RFC 7231, Section 6.3.3
203NON_AUTHORITATIVE_INFORMATIONHTTP/1.1 RFC 7231, Section 6.3.4
204NO_CONTENT: 没有内容HTTP/1.1 RFC 7231, Section 6.3.5
205RESET_CONTENTHTTP/1.1 RFC 7231, Section 6.3.6
206PARTIAL_CONTENTHTTP/1.1 RFC 7233, Section 4.1
207MULTI_STATUSWebDAV RFC 4918, Section 11.1
208ALREADY_REPORTEDWebDAV Binding Extensions RFC 5842, Section 7.1 (Experimental)
226IM_USEDDelta Encoding in HTTP RFC 3229, Section 10.4.1
300MULTIPLE_CHOICES:有多种资源可选择HTTP/1.1 RFC 7231, Section 6.4.1
301MOVED_PERMANENTLY:永久移动HTTP/1.1 RFC 7231, Section 6.4.2
302FOUND:临时移动HTTP/1.1 RFC 7231, Section 6.4.3
303SEE_OTHER:已经移动HTTP/1.1 RFC 7231, Section 6.4.4
304NOT_MODIFIED:没有修改HTTP/1.1 RFC 7232, Section 4.1
305USE_PROXY:使用代理HTTP/1.1 RFC 7231, Section 6.4.5
307TEMPORARY_REDIRECT:临时重定向HTTP/1.1 RFC 7231, Section 6.4.7
308PERMANENT_REDIRECT:永久重定向Permanent Redirect RFC 7238, Section 3 (Experimental)
400BAD_REQUEST:错误请求HTTP/1.1 RFC 7231, Section 6.5.1
401UNAUTHORIZED:未授权HTTP/1.1 Authentication RFC 7235, Section 3.1
402PAYMENT_REQUIRED:保留,将来使用HTTP/1.1 RFC 7231, Section 6.5.2
403FORBIDDEN:禁止HTTP/1.1 RFC 7231, Section 6.5.3
404NOT_FOUND:没有找到HTTP/1.1 RFC 7231, Section 6.5.4
405METHOD_NOT_ALLOWED:该请求方法不允许HTTP/1.1 RFC 7231, Section 6.5.5
406NOT_ACCEPTABLE:不可接受HTTP/1.1 RFC 7231, Section 6.5.6
407PROXY_AUTHENTICATION_REQUIRED:要求使用代理验明正身HTTP/1.1 Authentication RFC 7235, Section 3.2
408REQUEST_TIMEOUT:请求超时HTTP/1.1 RFC 7231, Section 6.5.7
409CONFLICT:冲突HTTP/1.1 RFC 7231, Section 6.5.8
410GONE:已经不在了HTTP/1.1 RFC 7231, Section 6.5.9
411LENGTH_REQUIRED:长度要求HTTP/1.1 RFC 7231, Section 6.5.10
412PRECONDITION_FAILED:前提条件错误HTTP/1.1 RFC 7232, Section 4.2
413REQUEST_ENTITY_TOO_LARGE:请求体太大了HTTP/1.1 RFC 7231, Section 6.5.11
414REQUEST_URI_TOO_LONG:请求URI太长了HTTP/1.1 RFC 7231, Section 6.5.12
415UNSUPPORTED_MEDIA_TYPE:不支持的媒体格式HTTP/1.1 RFC 7231, Section 6.5.13
416REQUESTED_RANGE_NOT_SATISFIABLEHTTP/1.1 Range Requests RFC 7233, Section 4.4
417EXPECTATION_FAILED:期望失败HTTP/1.1 RFC 7231, Section 6.5.14
421MISDIRECTED_REQUESTHTTP/2 RFC 7540, Section 9.1.2
422UNPROCESSABLE_ENTITY:可加工实体WebDAV RFC 4918, Section 11.2
423LOCKED:锁着WebDAV RFC 4918, Section 11.3
424FAILED_DEPENDENCY:失败的依赖WebDAV RFC 4918, Section 11.4
426UPGRADE_REQUIRED:升级需要HTTP/1.1 RFC 7231, Section 6.5.15
428PRECONDITION_REQUIRED:先决条件要求Additional HTTP Status Codes RFC 6585
429TOO_MANY_REQUESTS:太多的请求Additional HTTP Status Codes RFC 6585
431REQUEST_HEADER_FIELDS_TOO_LARGE:请求头太大Additional HTTP Status Codes RFC 6585
500INTERNAL_SERVER_ERROR:内部服务错误HTTP/1.1 RFC 7231, Section 6.6.1
501NOT_IMPLEMENTED:不可执行HTTP/1.1 RFC 7231, Section 6.6.2
502BAD_GATEWAY:无效网关HTTP/1.1 RFC 7231, Section 6.6.3
503SERVICE_UNAVAILABLE:服务不可用HTTP/1.1 RFC 7231, Section 6.6.4
504GATEWAY_TIMEOUT:网关超时HTTP/1.1 RFC 7231, Section 6.6.5
505HTTP_VERSION_NOT_SUPPORTED:HTTP版本不支持HTTP/1.1 RFC 7231, Section 6.6.6
506VARIANT_ALSO_NEGOTIATES:服务器存在内部配置错误透明内容协商在: HTTP RFC 2295, Section 8.1 (实验性的)
507INSUFFICIENT_STORAGE:存储不足WebDAV RFC 4918, Section 11.5
508LOOP_DETECTED:循环检测WebDAV Binding Extensions RFC 5842, Section 7.2 (Experimental)
510NOT_EXTENDED:不扩展An HTTP Extension Framework RFC 2774, Section 7 (Experimental)
511NETWORK_AUTHENTICATION_REQUIRED:要求网络身份验证Additional HTTP Status Codes RFC 6585, Section 6

为了保持向后兼容性,枚举值也以常量形式出现在 http.client 模块中,。 枚举名等于常量名 (例如 http.HTTPStatus.OK 也可以是 http.client.OK)。

在 3.7 版更改: 添加了 421 MISDIRECTED_REQUEST 状态码。