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.
Python code
30 linesimport 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
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
- Use `requests` library for a cleaner API and automatic redirect handling.
- 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
More from Automation & scripting
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
- Automatically Log CPU, RAM, and Disk Usage Every Minute in Python easy
- Batch Rename Hundreds of Files in Python easy
Keep learning
Related tutorials and quizzes for this topic.