Selected Reading

Python response.status_code Attribute



Response.status_code attribute of the Python Requests module indicates the status of the request. It is a three-digit integer where the first digit defines the response class such as 2xx for success, 4xx for client errors, 5xx for server errors etc.

The status code informs whether the request was successful or encountered an error, guiding further actions in program logic. A '200' status code typically signifies success while the codes like '404' indicate resource not found.

Handling these codes programmatically helps developers manage errors, troubleshoot network issues and ensure robust communication between applications and web services.

Syntax

Following is the syntax and parameters of Response.status_code attribute of the Python Requests module −

response.status_code

Parameter

This attribute does not accept any parameters.

Return value

This attribute returns the HTTP status code.

Example 1

Following is the example of Response.status_code attribute of the Python Requests module. Here in this example we will check for the given url and prints appropriate status code −

import requests

# Example URL
url = 'https://www.google.com'

# Send a GET request
response = requests.get(url)

# Check the status code
if response.status_code == 200:
    print("Request was successful!")
elif response.status_code == 404:
    print("Not Found. The requested resource could not be found.")
elif response.status_code == 401:
    print("Unauthorized. Access to the resource is restricted.")
else:
    print(f"Request failed with status code: {response.status_code}")

Output

Request was successful!

Example 2

When working with HTTP status codes in Python's requests library we can use constants provided by requests.codes for better readability and clarity. Here's how we can use these constants to handle different status codes −

import requests
from requests import codes

# Example URL
url = 'https://www.tutorialspoint.com/'

# Send a GET request
response = requests.get(url)

# Check the status code using constants
if response.status_code == codes.ok:
    print("Request was successful (200 OK)!")
elif response.status_code == codes.not_found:
    print("Not Found (404). The requested resource could not be found.")
elif response.status_code == codes.unauthorized:
    print("Unauthorized (401). Access to the resource is restricted.")
elif response.status_code == codes.forbidden:
    print("Forbidden (403). Access to the resource is forbidden.")
elif response.status_code == codes.internal_server_error:
    print("Internal Server Error (500). The server encountered an unexpected condition.")
else:
    print(f"Request failed with status code: {response.status_code}")

Output

Forbidden (403). Access to the resource is forbidden.

Example 3

When handling HTTP status codes in Python with the requests library it's often beneficial to provide detailed handling for common status codes to improve error reporting and application robustness. Here's an example of how we can implement detailed handling −

import requests

# Example URL
url = 'https://www.example.com'

# Send a GET request
response = requests.get(url)

# Detailed status code handling
if response.status_code == 200:
    print("Request was successful (200 OK)!")
    # Process the response data here if needed
elif response.status_code == 400:
    print("Bad Request (400). The server could not understand the request.")
    # Handle specific errors or provide user feedback
elif response.status_code == 401:
    print("Unauthorized (401). Access to the resource is restricted.")
    # Redirect to login or handle authentication issues
elif response.status_code == 403:
    print("Forbidden (403). Access to the resource is forbidden.")
    # Display a message or redirect based on permissions
elif response.status_code == 404:
    print("Not Found (404). The requested resource could not be found.")
    # Provide options to navigate or search for similar content
elif response.status_code == 500:
    print("Internal Server Error (500). The server encountered an unexpected condition.")
    # Notify user or log for investigation
else:
    print(f"Request failed with status code: {response.status_code}")
    # Handle other status codes not explicitly covered

Output

Request was successful (200 OK)!
python_modules.htm
Advertisements