http内置库(1)-HTTPStatus

本文详细介绍了HTTP协议中的各种状态码,包括成功响应、重定向、客户端错误和服务器错误,以及它们的含义和应用场景。
摘要由CSDN通过智能技术生成
# informational
CONTINUE = 100, 'Continue', 'Request received, please continue'
SWITCHING_PROTOCOLS = (101, 'Switching Protocols',
        'Switching to new protocol; obey Upgrade header')
PROCESSING = 102, 'Processing'
EARLY_HINTS = 103, 'Early Hints'

# success
OK = 200, 'OK', 'Request fulfilled, document follows'
CREATED = 201, 'Created', 'Document created, URL follows'
ACCEPTED = (202, 'Accepted',
    'Request accepted, processing continues off-line')
NON_AUTHORITATIVE_INFORMATION = (203,
    'Non-Authoritative Information', 'Request fulfilled from cache')
NO_CONTENT = 204, 'No Content', 'Request fulfilled, nothing follows'
RESET_CONTENT = 205, 'Reset Content', 'Clear input form for further input'
PARTIAL_CONTENT = 206, 'Partial Content', 'Partial content follows'
MULTI_STATUS = 207, 'Multi-Status'
ALREADY_REPORTED = 208, 'Already Reported'
IM_USED = 226, 'IM Used'

# redirection
MULTIPLE_CHOICES = (300, 'Multiple Choices',
    'Object has several resources -- see URI list')
MOVED_PERMANENTLY = (301, 'Moved Permanently',
    'Object moved permanently -- see URI list')
FOUND = 302, 'Found', 'Object moved temporarily -- see URI list'
SEE_OTHER = 303, 'See Other', 'Object moved -- see Method and URL list'
NOT_MODIFIED = (304, 'Not Modified',
    'Document has not changed since given time')
USE_PROXY = (305, 'Use Proxy',
    'You must use proxy specified in Location to access this resource')
TEMPORARY_REDIRECT = (307, 'Temporary Redirect',
    'Object moved temporarily -- see URI list')
PERMANENT_REDIRECT = (308, 'Permanent Redirect',
    'Object moved permanently -- see URI list')

# client error
BAD_REQUEST = (400, 'Bad Request',
    'Bad request syntax or unsupported method')
UNAUTHORIZED = (401, 'Unauthorized',
    'No permission -- see authorization schemes')
PAYMENT_REQUIRED = (402, 'Payment Required',
    'No payment -- see charging schemes')
FORBIDDEN = (403, 'Forbidden',
    'Request forbidden -- authorization will not help')
NOT_FOUND = (404, 'Not Found',
    'Nothing matches the given URI')
METHOD_NOT_ALLOWED = (405, 'Method Not Allowed',
    'Specified method is invalid for this resource')
NOT_ACCEPTABLE = (406, 'Not Acceptable',
    'URI not available in preferred format')
PROXY_AUTHENTICATION_REQUIRED = (407,
    'Proxy Authentication Required',
    'You must authenticate with this proxy before proceeding')
REQUEST_TIMEOUT = (408, 'Request Timeout',
    'Request timed out; try again later')
CONFLICT = 409, 'Conflict', 'Request conflict'
GONE = (410, 'Gone',
    'URI no longer exists and has been permanently removed')
LENGTH_REQUIRED = (411, 'Length Required',
    'Client must specify Content-Length')
PRECONDITION_FAILED = (412, 'Precondition Failed',
    'Precondition in headers is false')
REQUEST_ENTITY_TOO_LARGE = (413, 'Request Entity Too Large',
    'Entity is too large')
REQUEST_URI_TOO_LONG = (414, 'Request-URI Too Long',
    'URI is too long')
UNSUPPORTED_MEDIA_TYPE = (415, 'Unsupported Media Type',
    'Entity body in unsupported format')
REQUESTED_RANGE_NOT_SATISFIABLE = (416,
    'Requested Range Not Satisfiable',
    'Cannot satisfy request range')
EXPECTATION_FAILED = (417, 'Expectation Failed',
    'Expect condition could not be satisfied')
IM_A_TEAPOT = (418, 'I\'m a Teapot',
    'Server refuses to brew coffee because it is a teapot.')
MISDIRECTED_REQUEST = (421, 'Misdirected Request',
    'Server is not able to produce a response')
UNPROCESSABLE_ENTITY = 422, 'Unprocessable Entity'
LOCKED = 423, 'Locked'
FAILED_DEPENDENCY = 424, 'Failed Dependency'
TOO_EARLY = 425, 'Too Early'
UPGRADE_REQUIRED = 426, 'Upgrade Required'
PRECONDITION_REQUIRED = (428, 'Precondition Required',
    'The origin server requires the request to be conditional')
TOO_MANY_REQUESTS = (429, 'Too Many Requests',
    'The user has sent too many requests in '
    'a given amount of time ("rate limiting")')
REQUEST_HEADER_FIELDS_TOO_LARGE = (431,
    'Request Header Fields Too Large',
    'The server is unwilling to process the request because its header '
    'fields are too large')
UNAVAILABLE_FOR_LEGAL_REASONS = (451,
    'Unavailable For Legal Reasons',
    'The server is denying access to the '
    'resource as a consequence of a legal demand')

# server errors
INTERNAL_SERVER_ERROR = (500, 'Internal Server Error',
    'Server got itself in trouble')
NOT_IMPLEMENTED = (501, 'Not Implemented',
    'Server does not support this operation')
BAD_GATEWAY = (502, 'Bad Gateway',
    'Invalid responses from another server/proxy')
SERVICE_UNAVAILABLE = (503, 'Service Unavailable',
    'The server cannot process the request due to a high load')
GATEWAY_TIMEOUT = (504, 'Gateway Timeout',
    'The gateway server did not receive a timely response')
HTTP_VERSION_NOT_SUPPORTED = (505, 'HTTP Version Not Supported',
    'Cannot fulfill request')
VARIANT_ALSO_NEGOTIATES = 506, 'Variant Also Negotiates'
INSUFFICIENT_STORAGE = 507, 'Insufficient Storage'
LOOP_DETECTED = 508, 'Loop Detected'
NOT_EXTENDED = 510, 'Not Extended'
NETWORK_AUTHENTICATION_REQUIRED = (511,
    'Network Authentication Required',
    'The client needs to authenticate to gain network access')

定义了一个名为 HTTPStatus 的 Python 类,该类继承自 IntEnum,这是 Python 内置 enum 模块中的一个子类,用于创建具有整数值的枚举类型。这个特定的 HTTPStatus 类是为了表示 HTTP 协议中的所有状态码及其相关的描述(原因短语)和详细说明。


通过这种方式组织,开发人员可以轻松地在他们的 HTTP 相关代码中使用这些枚举值,增加代码的可读性和安全性,因为这样可以防止由于手动输入状态码数字导致的错误,并且 IDE 和静态分析工具也能更好地识别和提示这些状态码。


## 2、示例



from your_module import HTTPStatus

创建一个 HTTPStatus 的实例

status = HTTPStatus.OK

访问状态码数值

print(status.value) # 输出:200

访问原因短语

print(status.phrase) # 输出:“OK”

访问详细描述

print(status.description) # 输出:“Request fulfilled, document follows”

也可以通过名字获取状态码

status = HTTPStatus.NOT_FOUND

输出状态码及其相关信息

print(status.name, status.value, status.phrase, status.description)

输出:“NOT_FOUND 404 Not Found Nothing matches the given URI”


## 3、核心代码解析



def __new__(cls, value, phrase, description=‘’):
obj = int.new(cls, value)
obj.value = value

    obj.phrase = phrase
    obj.description = description
    return obj

 **自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

![img](https://img-blog.csdnimg.cn/img_convert/b9949953afe82071f662c5683c3b58cf.png)

![img](https://img-blog.csdnimg.cn/img_convert/cdcde4a060466a1a0c4261826a18db4d.png)

![img](https://img-blog.csdnimg.cn/img_convert/e67ec087cb928ef28c2f9bd800dcc9f6.png)

![img](https://img-blog.csdnimg.cn/img_convert/403998029758bea24646932ce2ebfafe.png)

![img](https://img-blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)

![img](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)**

)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)**

![](https://img-blog.csdnimg.cn/img_convert/bda69c928c65cf41134dc553cc085108.jpeg)
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值