在Python请求中获取状态代码的描述

时间:2022-03-29 23:18:58

I would like to be able to enter a server response code and have Requests tell me what the code means. For example, code 200 --> ok

我希望能够输入服务器响应代码并让请求告诉我代码的含义。例如,代码200 - >确定

I found a link to the source code which shows the dictionary structure of the codes and descriptions. I see that Requests will return a response code for a given description:

我找到了源代码的链接,它显示了代码和描述的字典结构。我看到Requests将返回给定描述的响应代码:

print requests.codes.processing  # returns 102print requests.codes.ok          # returns 200print requests.codes.not_found   # returns 404

But not the other way around:

但不是相反:

print requests.codes[200]        # returns Noneprint requests.codes.viewkeys()  # returns dict_keys([])print requests.codes.keys()      # returns []

I thought this would be a routine task, but cannot seem to find an answer to this in online searching, or in the documentation.

我认为这将是一项例行任务,但似乎无法在在线搜索或文档中找到答案。

3 个解决方案

#1


13  

One possibility:

>>> import requests>>> requests.status_codes._codes[200]('ok', 'okay', 'all_ok', 'all_okay', 'all_good', '\\o/', '\xe2\x9c\x93')

The first value in the tuple is used as the conventional code key.

元组中的第一个值用作传统的代码密钥。

#2


15  

Alternatively, in case of Python 2.x, you can use httplib.responses:

或者,对于Python 2.x,您可以使用httplib.responses:

>>> import httplib>>> httplib.responses[200]'OK'>>> httplib.responses[404]'Not Found'

In Python 3.x, use http module:

在Python 3.x中,使用http模块:

In [1]: from http.client import responsesIn [2]: responses[200]Out[2]: 'OK'In [3]: responses[404]Out[3]: 'Not Found'

#3


0  

requests.status_codes.codes.OK

works nicely and makes it more readable in my application code

很好地工作,使我的应用程序代码更具可读性

Notice that in source code: the requests.status_codes.codes is of type LookupDict which overrides method getitem

请注意,在源代码中:requests.status_codes.codes是LookupDict类型,它覆盖方法getitem

You could see all the supported keys with - dir(requests.status_codes.codes)

你可以看到所有支持的键--dir(requests.status_codes.codes)

When using in combination with FLASK:

i like use following enum from flask-api pluginfrom flask_api import status where i get more descriptive version of HTTP status codes as in -

我喜欢使用来自flask-api插件的enum来自于flask_api导入状态,其中我获得了更多描述性的HTTP状态代码版本,如下所示 -

status.HTTP_200_OK

#1


13  

One possibility:

>>> import requests>>> requests.status_codes._codes[200]('ok', 'okay', 'all_ok', 'all_okay', 'all_good', '\\o/', '\xe2\x9c\x93')

The first value in the tuple is used as the conventional code key.

元组中的第一个值用作传统的代码密钥。

#2


15  

Alternatively, in case of Python 2.x, you can use httplib.responses:

或者,对于Python 2.x,您可以使用httplib.responses:

>>> import httplib>>> httplib.responses[200]'OK'>>> httplib.responses[404]'Not Found'

In Python 3.x, use http module:

在Python 3.x中,使用http模块:

In [1]: from http.client import responsesIn [2]: responses[200]Out[2]: 'OK'In [3]: responses[404]Out[3]: 'Not Found'

#3


0  

requests.status_codes.codes.OK

works nicely and makes it more readable in my application code

很好地工作,使我的应用程序代码更具可读性

Notice that in source code: the requests.status_codes.codes is of type LookupDict which overrides method getitem

请注意,在源代码中:requests.status_codes.codes是LookupDict类型,它覆盖方法getitem

You could see all the supported keys with - dir(requests.status_codes.codes)

你可以看到所有支持的键--dir(requests.status_codes.codes)

When using in combination with FLASK:

i like use following enum from flask-api pluginfrom flask_api import status where i get more descriptive version of HTTP status codes as in -

我喜欢使用来自flask-api插件的enum来自于flask_api导入状态,其中我获得了更多描述性的HTTP状态代码版本,如下所示 -

status.HTTP_200_OK