Check Web Traffic Using Python

Anthony M
2 min readMay 10, 2024
Photo by Vinh Dang: https://www.pexels.com/photo/photo-of-gray-airplane-946071/

To check web traffic using Python, you can utilize the requests library to send HTTP requests to a website and retrieve its response. Below is a basic Python script that demonstrates how to check web traffic by sending a GET request to a specified URL:

import requests

def check_web_traffic(url):
try:
response = requests.get(url)
if response.status_code == 200:
print(f"Web traffic for {url} is good.")
else:
print(f"Web traffic for {url} is not as expected. Status code: {response.status_code}")
except requests.RequestException as e:
print(f"An error occurred: {e}")

if __name__ == "__main__":
url = input("Enter the URL to check web traffic: ")
check_web_traffic(url)

This script prompts the user to input a URL, then sends a GET request to that URL using requests.get(). It checks the response status code, where a status code of 200 indicates a successful response. If the status code is 200, it prints that the web traffic is good. Otherwise, it prints that the web traffic is not as expected along with the status code.

Make sure you have the requests library installed. You can install it via pip if you haven't already:

pip install requests

You can expand this script to perform more complex checks or to gather additional information about web traffic, such as response time or specific content on the webpage.

--

--