Categories We Write About

Create a link health checker

Creating a link health checker involves building a tool that can scan a list of URLs and verify whether those links are working (returning valid HTTP responses) or broken (returning errors or no response). Here’s a simple example of how to build one in Python, which can be easily adapted for web or command-line use.


python
import requests def check_link_health(urls, timeout=5): """ Check the health of a list of URLs. Args: urls (list): List of URL strings to check. timeout (int): Timeout for each request in seconds. Returns: dict: Dictionary with URL as key and status as value. """ results = {} headers = { 'User-Agent': 'LinkHealthChecker/1.0' } for url in urls: try: response = requests.head(url, timeout=timeout, headers=headers, allow_redirects=True) # If HEAD is not allowed, fall back to GET if response.status_code >= 400 or response.status_code == 405: response = requests.get(url, timeout=timeout, headers=headers, allow_redirects=True) status_code = response.status_code if 200 <= status_code < 400: results[url] = f"Healthy (Status code: {status_code})" else: results[url] = f"Broken (Status code: {status_code})" except requests.exceptions.RequestException as e: results[url] = f"Error ({e})" return results # Example usage: if __name__ == "__main__": urls_to_check = [ "https://www.google.com", "https://www.example.com/nonexistentpage", "https://thiswebsitedoesnotexist12345.com" ] health_report = check_link_health(urls_to_check) for url, status in health_report.items(): print(f"{url}: {status}")

Explanation:

  • The script uses requests.head() first for faster checks.

  • If HEAD is disallowed or returns error, falls back to GET.

  • Handles redirects with allow_redirects=True.

  • Considers HTTP status codes in 200–399 range as healthy.

  • Catches exceptions (timeout, DNS failures, etc.) and reports errors.

  • Easily expandable to read URLs from files or databases.


If you want, I can also provide a version in JavaScript (Node.js) or a simple front-end implementation!

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About