Saturday, May 13, 2023

Acquiring HTTPS status of URL/Endpoint in Python and displaying informative messages based on the received status code

 To be an expert Python and application developer, it is essential to understand and effectively handle HTTPS status codes while working with web applications in Python. These codes are part of the HTTP response and provide valuable information about the outcome of a request made to a specific URL or endpoint. HTTPS status codes are three-digit numbers, where the first digit represents the category of the response. The categories are:


1xx (Informational): The request was received, and the server continues to process it.

2xx (Successful): The request was successfully received, understood, and accepted.

3xx (Redirection): Further action needs to be taken to complete the request.

4xx (Client Error): The request contains bad syntax or cannot be fulfilled by the server.

5xx (Server Error): The server failed to fulfill a valid request.

Understanding these status codes helps developers:


Determine if the request was successful or not.

Handle errors or exceptions that may occur due to issues with the request or server.

Provide informative messages to users based on the outcome of the request.

Implement conditional logic to perform different actions depending on the status code.

Some common HTTPS status codes include:


200 OK: The request was successful, and the server has returned the requested data.

201 Created: The request was successful, and the server has created a new resource as a result (common for successful POST requests).

204 No Content: The request was successful, but there is no data to return (common for DELETE requests).

400 Bad Request: The server could not understand the request due to invalid syntax or missing required data.

401 Unauthorized: The request requires user authentication.

403 Forbidden: The server understood the request but refuses to authorize it.

404 Not Found: The server can’t find the requested resource.

500 Internal Server Error: The server has encountered a situation it doesn’t know how to handle.

You can find a comprehensive list of HTTP status codes on the official website of the Internet Assigned Numbers Authority (IANA): https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml


In Python we have two main libraries requests and http.client for making HTTP requests. Both differ in terms of their features, simplicity, and ease of use.


requests is a popular third-party library that simplifies the process of making HTTP requests in Python. It provides a user-friendly and intuitive interface for working with HTTP requests and responses. http.client is part of the Python standard library and provides a low-level interface for working with HTTP requests and responses. It doesn’t offer as many features as requests, and you’ll need to handle redirects, cookies, and other advanced features manually.


Now let me show some examples for handling HTTPs code in Python. Below is an simple example that uses the Python’s requests library to make an HTTP GET request and print the response code in Python:


import requests


response = requests.get(‘https://www.example.com’)


response_code = response.status_code


print(f’Response code: {response_code}’)


Now let me show you another Python code that will capture the response from URL and print relevant messages based on the status codes.


import requests


response = requests.get(‘https://www.example.com')


# Check the status code

if response.status_code == 200:

print(“Request was successful!”)

# Process the response content

content = response.text

print(content)

elif response.status_code == 404:

print(“The requested resource was not found.”)

else:

print(f”An error occurred. Status code: {response.status_code}”)

No comments: