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.

Easy Python 3.9+ Aug 9, 2026 Automation & scripting 12 views 0 copies

Requires third-party packages — install first
pip install requests

Python code

23 lines
Python 3.9+
import 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

stdout
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

  1. Use `asyncio` with `aiohttp` for async HTTP requests instead of threads.
  2. 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

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Automation & scripting

Related tutorials and quizzes for this topic.