Website Monitor
A zero-dependency Python tool for checking website availability, measuring response times, and detecting content changes. Uses only Python's standard library (urllib) — no requests, no aiohttp, no external packages.
Features
- - Uptime checking: Verify any URL returns the expected HTTP status code
- Response time measurement: Precise millisecond timing for each request
- Content change detection: SHA-256 based hashing to detect when page content changes between checks
- Text verification: Confirm that specific text appears in the response body
- Multi-URL support: Check multiple sites in a single command
- JSON output: Machine-readable output for integration with other tools and dashboards
- Exit codes: Returns exit code 1 if any site is down, making it perfect for shell scripts and cron jobs
Usage Examples
Simple uptime check:
CODEBLOCK0
Check multiple sites at once:
CODEBLOCK1
Detect content changes (compare against a previous hash):
CODEBLOCK2
Verify a page contains expected text:
CODEBLOCK3
Expect a specific status code (e.g., redirect):
CODEBLOCK4
JSON output for scripting:
CODEBLOCK5
Use in a cron job or script:
CODEBLOCK6
Command Line Options
- -
urls — One or more URLs to check (auto-prepends https:// if missing) - INLINECODE1 — Request timeout in seconds (default: 10)
- INLINECODE2 — Expected HTTP status code (default: 200)
- INLINECODE3 — Verify response body contains this string
- INLINECODE4 — Previous content hash to compare against for change detection
- INLINECODE5 — Output results as JSON array
Exit Codes
- -
0 — All sites are up and match expectations - INLINECODE7 — One or more sites are down or failed checks
网站监控器
一个零依赖的Python工具,用于检查网站可用性、测量响应时间以及检测内容变化。仅使用Python标准库(urllib)——无需requests、aiohttp或任何外部包。
功能特性
- - 运行状态检查:验证任意URL是否返回预期的HTTP状态码
- 响应时间测量:每次请求的精确毫秒级计时
- 内容变化检测:基于SHA-256哈希算法,检测两次检查间页面内容是否发生变化
- 文本验证:确认响应正文中包含特定文本
- 多URL支持:单条命令检查多个站点
- JSON输出:机器可读的输出格式,便于与其他工具和仪表盘集成
- 退出码:若任一站点宕机则返回退出码1,完美适用于Shell脚本和定时任务
使用示例
简单运行状态检查:
bash
python main.py https://example.com
✅ https://example.com
状态:200
响应:142ms
大小:1256字节
哈希:fb91d75a6bb43078
同时检查多个站点:
bash
python main.py example.com google.com github.com
检测内容变化(与之前的哈希值对比):
bash
python main.py https://example.com --hash-check fb91d75a6bb43078
显示已变化:是 ⚠️ 或 未变化:否
验证页面包含预期文本:
bash
python main.py https://status.example.com --contains 所有系统正常运行
期望特定状态码(例如重定向):
bash
python main.py https://old.example.com --expect 301
脚本化JSON输出:
bash
python main.py example.com github.com --json | jq .[] | select(.up == false)
用于定时任务或脚本:
bash
python main.py https://mysite.com || echo 警报:站点宕机! | mail -s 宕机警报 admin@example.com
命令行选项
- - urls — 一个或多个待检查的URL(若缺少协议则自动添加https://)
- --timeout N — 请求超时时间(秒,默认:10)
- --expect N — 预期的HTTP状态码(默认:200)
- --contains TEXT — 验证响应正文包含此字符串
- --hash-check HASH — 用于变化检测对比的先前内容哈希值
- --json — 以JSON数组格式输出结果
退出码
- - 0 — 所有站点正常运行且符合预期
- 1 — 一个或多个站点宕机或检查失败