Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

How to Build a Python Script That Checks Website Response Time

Hello, I'm Arthur. When working on websites, one of the first things I like to check is how quickly a page responds. A website can look perfectly fine but still take several seconds to load. This can affect the user exp

Hello, I'm Arthur. When working on websites, one of the first things I like to check is how quickly a page responds.

A website can look perfectly fine but still take several seconds to load. This can affect the user experience, especially when someone is trying to access an important page.

Instead of checking every URL manually, you can use Python to build a simple website response time checker.

In this tutorial, we'll create a script that checks multiple URLs, measures their response times, and saves the results to a CSV file.

1. Install the Required Python Library

We'll use the requests library to send HTTP requests and measure how long each request takes.

Install it using pip:

pip install requests

We'll also use Python's built-in time and csv modules, so there is no need to install additional packages for those.

2. Create a Simple Response Time Checker

Create a new Python file called website_checker.py.

Start with the following code:

import time
import requests

url = "https://example.com"

start_time = time.perf_counter()

response = requests.get(url, timeout=10)

end_time = time.perf_counter()

response_time = end_time - start_time

print("Status code:", response.status_code)
print("Response time:", round(response_time, 3), "seconds")

The script sends a request to the website and measures the time taken to receive the response.

The status code tells you whether the request was successful or returned an error.

The response time gives you an idea of how quickly the server responded.

3. Check Multiple Websites

Checking one website is useful, but you can make the script more practical by checking several URLs.

import time
import requests

urls = [
    "https://example.com",
    "https://www.python.org",
    "https://www.wikipedia.org"
]

for url in urls:
    try:
        start_time = time.perf_counter()

        response = requests.get(url, timeout=10)

        end_time = time.perf_counter()

        response_time = end_time - start_time

        print(url)
        print("Status:", response.status_code)
        print("Response time:", round(response_time, 3))
        print()

    except requests.RequestException as error:
        print(url, "Error:", error)

This version checks each URL and prints its response time.

The try and except blocks help the script continue even if one website cannot be reached.

4. Save the Results to a CSV File

If you want to compare website performance over time, saving the results is a useful next step.

You can use Python's built-in csv module.

import csv
import time
import requests

urls = [
    "https://example.com",
    "https://www.python.org"
]

with open("website_results.csv", "w", newline="") as file:
    writer = csv.writer(file)

    writer.writerow([
        "Website",
        "Status Code",
        "Response Time"
    ])

    for url in urls:
        try:
            start_time = time.perf_counter()

            response = requests.get(url, timeout=10)

            end_time = time.perf_counter()

            response_time = round(
                end_time - start_time, 3
            )

            writer.writerow([
                url,
                response.status_code,
                response_time
            ])

        except requests.RequestException:
            writer.writerow([
                url,
                "Error",
                "N/A"
            ])

After running the script, you'll find a file named website_results.csv.

You can open it in Excel or Google Sheets to compare the results.

5. Understand What Response Time Actually Means

One important thing to remember is that this script measures the time taken for an HTTP request to complete.

It does not measure every part of a visitor's experience.

For example, a browser may need additional time to download images, execute JavaScript, or render a page.

The response time can also change depending on network conditions, server load, and the location from which the request is made.

If you're comparing hosting options, it's useful to test from a consistent location and repeat the measurements.

For projects that need reliable server performance, you can also compare HelloServer's hosting services and consider the hardware and network requirements of your application.

Final Thoughts

Building a website response time checker is a practical way to improve your Python skills.

It teaches you how to send HTTP requests, measure execution time, handle errors, and save data for later analysis.

You can extend this project by checking response times at regular intervals or creating a graph from the collected results.

Start with a few URLs, understand the results, and gradually add more features as your skills improve.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.