How to Check Website Status Codes in Python
This script checks the HTTP status codes of multiple URLs concurrently using a thread pool and prints the results.
pip install requests
Python code
23 linesimport requests
from concurrent.futures import ThreadPoolExecutor
URLS = [
"https://www.google.com",
"https://www.python.org",
"https://www.nonexistent-site-12345.com",
"https://www.github.com",
]
def check_status(url):
try:
response = requests.get(url, timeout=5)
return url, response.status_code
except requests.RequestException:
return url, "ERROR"
with ThreadPoolExecutor(max_workers=4) as executor:
results = list(executor.map(check_status, URLS))
if __name__ == "__main__":
for url, status in results:
print(f"{url} -> {status}")
Output
https://www.google.com -> 200
https://www.python.org -> 200
https://www.nonexistent-site-12345.com -> ERROR
https://www.github.com -> 200
How it works
The requests.get call returns a response object whose status_code attribute holds the HTTP status code. A ThreadPoolExecutor runs the check_status function concurrently for each URL, speeding up the process compared to sequential requests. The timeout=5 argument prevents the script from hanging on slow or unresponsive servers. Exceptions like connection errors are caught by requests.RequestException, so a failed request returns the string "ERROR" instead of crashing the script.
Common mistakes
- Forgetting to set a timeout on the `requests.get` call, which can cause the script to hang.
- Catching only `ConnectionError` instead of the broader `requests.RequestException`.
- Not handling the case where the URL list is empty, which would produce an empty result list.
- Using sequential requests for a large list, making the script unnecessarily slow.
Variations
- Use `asyncio` with `aiohttp` for async HTTP requests instead of threads.
- Group URLs by status code using a dictionary for easier analysis.
Real-world use cases
- Monitoring a fleet of internal microservices to detect which ones are unhealthy after a deployment.
- Running a periodic cron job that alerts the team when any public API endpoint returns 5xx.
- Batch-checking a list of partner websites for link rot in a content pipeline.
Sponsored
More from Automation & scripting
- Aggregate Log Errors Count by Hour in Python easy
- Automate Tweeting New Blog Posts in Python easy
- 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
Keep learning
Related tutorials and quizzes for this topic.