在Python请求中获取状态代码的描述 [英] Get the description of a status code in Python Requests

查看:114
本文介绍了在Python请求中获取状态代码的描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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:

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

但不是相反:

print requests.codes[200]        # returns None
print 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.

推荐答案

一种可能性:

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

这篇关于在Python请求中获取状态代码的描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆