O'Reilly logo

Python Requests Essentials by Bala Subrahmanyam Varanasi, Rakesh Vidya Chandra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Looking up built-in response status codes

Status codes are helpful in letting us know the result, once a request is sent. To know about this, we can use status_code:

>>> r = requests.get('http://google.com')
>>> r.status_code
200

To make it much easier to deal with status_codes, Requests has got a built-in status code lookup object which serves as an easy reference. We must compare the requests.codes.ok with r.status_code to achieve this. If the result turns out to be True, then it's 200 status code, and if it's False, it's not. We can also compare the r.status.code with requests.codes.ok, requests.code.all_good to get the lookup work.

>>> r = requests.get('http://google.com')
>>> r.status_code == requests.codes.ok
True

Now, let's try checking with ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required