Build a Python Website Uptime Monitor That Alerts You Before Users Notice
Every site owner has had the same bad experience: a friend messages “hey, your site is down” hours after it actually broke. Professional monitoring tools exist, but a simple Python monitor you write yourself teaches the fundamentals — and it works perfectly well for a personal site or a small project. Let’s build one.
What It Will Do
Every few minutes, the script will request each site on your list, record the HTTP status code and response time, and log any failure. If a site fails twice in a row, it prints a loud alert. You can later swap the print for an email or a chat message — the checking logic stays the same.
The Code
import time
import requests
from datetime import datetime
SITES = ["https://example.com", "https://example.com/blog"]
CHECK_EVERY_SECONDS = 300
TIMEOUT_SECONDS = 10
FAILURES_BEFORE_ALERT = 2
failures = {site: 0 for site in SITES}
def check(site):
try:
r = requests.get(site, timeout=TIMEOUT_SECONDS)
return r.status_code, round(r.elapsed.total_seconds(), 2)
except requests.RequestException as e:
return None, str(e)
while True:
for site in SITES:
status, detail = check(site)
stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if status == 200:
failures[site] = 0
print(f"[{stamp}] OK {site} ({detail}s)")
else:
failures[site] += 1
print(f"[{stamp}] FAIL {site} -> {status} ({detail}) [{failures[site]}/{FAILURES_BEFORE_ALERT}]")
if failures[site] >= FAILURES_BEFORE_ALERT:
print(f"*** ALERT: {site} is DOWN ***")
time.sleep(CHECK_EVERY_SECONDS)
Save it as uptime.py, install the one dependency with pip install requests, and run python3 uptime.py. It will check your sites every five minutes until you stop it.
How It Works
- Timeouts matter. Without a timeout, a hung server would freeze your monitor forever. Ten seconds is a sensible ceiling for a simple check.
- Two failures before alerting. A single failed request is often a network blip. Requiring two consecutive failures filters out most false alarms while still alerting within ten minutes.
- Response time is logged too. A site returning 200 but taking 20 seconds is technically “up” and practically broken. Watching response times catches slowdowns before they become outages.
- Status 200 isn’t the whole story. Some broken sites return 200 with an error page. For a critical page, check that the response contains an expected phrase as well as the right status code.
Level Up
- Run it in the background. Use a systemd service or a terminal multiplexer like tmux so it survives you closing the terminal.
- Add real alerts. Replace the print with an email via smtplib, or a webhook to your chat app, so a 3am outage actually wakes you.
- Log to a file. Append results to uptime.log instead of printing, and you get a history you can graph later.
- Check certificates too. A site with an expired certificate is down for most visitors even though the server answers. Pair this monitor with an SSL expiry check for full coverage.
The Golden Rule
Only monitor sites you own or have permission to check. Hammering someone else’s server with frequent automated requests without permission is at best rude and at worst abusive. Your own sites, your own lab, your own projects — that’s the scope. A 50-line script, running quietly in the background, is the difference between “I fixed it before anyone noticed” and “why didn’t anyone tell me?” Build it today; your future self will be grateful.



Comments
Post a Comment