Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

http - Get the description of a status code in Python Requests

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

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

But not the other way around:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

In Python 3.x, use http module:

In [1]: from http.client import responses

In [2]: responses[200]
Out[2]: 'OK'

In [3]: responses[404]
Out[3]: 'Not Found'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...