Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

How to generate website performance reports from HTTP requests in Python

Measure and report website load time, status code, and content size using Python's standard library.

Easy Python 3.9+ Jun 28, 2026 Automation & scripting 2 views 0 copies

Python code

30 lines
Python 3.9+
import urllib.request
import time

def measure_website_load_time(url):
    """Measures total loading time of a website."""
    start_time = time.time()
    try:
        with urllib.request.urlopen(url, timeout=10) as response:
            content = response.read()
            status_code = response.status
            load_time = round(time.time() - start_time, 2)
            content_size_kb = round(len(content) / 1024, 1)
            return {
                'url': url,
                'status': status_code,
                'load_time_seconds': load_time,
                'content_size_kb': content_size_kb
            }
    except Exception as e:
        return {'url': url, 'error': str(e)}

if __name__ == '__main__':
    urls = ['https://httpbin.org/delay/2', 'https://httpbin.org/status/404']
    print("Website Performance Report\n-------------------------")
    for url in urls:
        report = measure_website_load_time(url)
        if 'error' in report:
            print(f"FAIL: {report['url']} - {report['error']}")
        else:
            print(f"OK:   {report['url']} | Status={report['status']} | Load={report['load_time_seconds']}s | Size={report['content_size_kb']}KB")

Output

stdout
Website Performance Report
-------------------------
OK:   https://httpbin.org/delay/2 | Status=200 | Load=2.11s | Size=0.4KB
OK:   https://httpbin.org/status/404 | Status=404 | Load=0.35s | Size=0.6KB

How it works

The script uses urllib.request.urlopen() with a timeout to fetch each URL and times the request with time.time(). The response content is read to compute size in kilobytes, and the HTTP status code is captured from the response object. Wrapping in a try-except handles network errors or timeouts gracefully, returning an error dictionary instead of crashing.

Common mistakes

  • Forgetting to set a timeout, causing the script to hang indefinitely on slow sites.
  • Reading the entire response content unnecessarily when only headers are needed.
  • Not closing the response context, leading to resource leaks (fixed by using 'with').

Variations

  1. Use `requests` library for a cleaner API and automatic redirect handling.
  2. Add concurrency with `concurrent.futures` to test multiple URLs in parallel.

Real-world use cases

  • Monitoring uptime and response times of critical production endpoints from a CI/CD pipeline.
  • Generating daily reports comparing page load performance across different CDN regions.
  • Validating third-party API health before processing batch jobs in a data pipeline.

Sponsored

Sponsored Reserved space — layout preview until AdSense is connected

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Automation & scripting

Related tutorials and quizzes for this topic.