Python Requests: Check Status Code and Description

“Requests” is a powerful and widely used library for making HTTP requests in Python. It provides a simple and easy-to-use interface for sending HTTP/1.1 requests and receiving responses from websites online. One of the most important features of the Python Requests library is the ability to check the status code and description of a response.


What is a Status Code?

HTTP status codes are three-digit numbers that indicate the status of an HTTP request. Each status code is associated with a description that gives more information about the status. The most common status codes are:

  • 200 OK – The request was successful.
  • 400 Bad Request – The request was malformed or invalid.
  • 401 Unauthorized – The request requires authentication.
  • 403 Forbidden – The server refused to fulfill the request.
  • 404 Not Found – The requested resource could not be found.
  • 500 Internal Server Error – The server encountered an unexpected condition.

How to check Status Code and its description using Requests

The “.status_code” attribute is used to check the status code of an HTTP response. It returns a three-digit integer that indicates the status of the response. To use this attribute, you simply call it on the response object.

Here’s an example:

import requests

response = requests.get('https://www.example.com')
print(response.status_code)
200

This code will send an HTTP GET request to “https://www.example.com” and print the status code of the response. This url is actually valid, that’s why it returns a “200” status code.


Check description of Status Code in Requests

While basic status codes like “200” and “404” are very well-known, we may get confused with some of the other uncommon ones. It’s useful to use something like the “.reason” property which is used to check the description of an HTTP response. It returns a string that gives more information about the status code.

Here is an example.

import requests

response = requests.get('https://www.example.com')
print(response.status_code, response.reason)
200 OK

Let’s try this on an invalid URL that does not exist.

import requests

response = requests.get('https://www.example.com/does/not/exist')
print(response.status_code, response.reason)
404 Not Found

The above status code tells us that the URL (page) was not found (does not exist).


Let’s try this one more time. The below example tries to connect to “amazon.com”.

import requests

response = requests.get('https://amazon.com')
print(response.status_code, response.reason)
503 Service Unavailable

Why did this website return a 503 error? This is because we got blocked by amazon! Interested in knowing how to avoid this? Follow the link!


This marks the end of the “Python Requests: How to check Status Code and Description” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be made in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments