Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 931
Roberto Avatar asked Jul 13 '14 00:07

Roberto


People also ask

How do I check Python Request status?

status_code returns a number that indicates the status (200 is OK, 404 is Not Found). Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object.

How do I check my HTTP request status code?

In the main window of the program, enter your website homepage URL and click on the 'Start' button. As soon as crawling is complete, you will have all status codes in the corresponding table column. Pages with the 4xx and 5xx HTTP status codes will be gathered into special issue reports.

What is status code in Python?

A status code informs you of the status of the request. For example, a 200 OK status means that your request was successful, whereas a 404 NOT FOUND status means that the resource you were looking for was not found.


2 Answers

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' 
like image 126
alecxe Avatar answered Sep 20 '22 23:09

alecxe


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.

like image 28
shux Avatar answered Sep 20 '22 23:09

shux