Skip to content

Instantly share code, notes, and snippets.

@esemi
Created May 30, 2025 15:33
Show Gist options
  • Select an option

  • Save esemi/a8db18821c06c041216376b54ec75537 to your computer and use it in GitHub Desktop.

Select an option

Save esemi/a8db18821c06c041216376b54ec75537 to your computer and use it in GitHub Desktop.
"""
External Host Availability Checker API.
A lightweight public API service for checking the alive state of external hostnames.
This module provides functionality to verify and monitor the availability status
of remote hosts through HTTP/HTTPS protocols.
"""
import requests
from fastapi import FastAPI
from requests.exceptions import ConnectionError, InvalidURL
app = FastAPI()
@app.get("/healthz")
async def healthz(hostname) -> dict:
"""
Checks if the host is up or down.
:param hostname: The name of the host being checked.
"""
print('request', hostname)
status = 'up' if is_host_alive(hostname) else 'down'
print('response', status)
return {'status': status, 'hostname': hostname}
def is_host_alive(h):
url = 'http://' + h
try:
response = requests.get(url, timeout=60 * 60 * 6)
status_code = response.status_code
if status_code >= 500:
return True
return False
except ConnectionError:
return False
except InvalidURL:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment