在日常运维和开发工作中,我们经常会遇到这样一个问题:系统上线之后,需要验证几十甚至上百个接口是否都能正常访问。手动一个一个去点、去测,不仅效率低,而且容易漏掉关键接口。特别是在微服务架构下,接口数量庞大,接口的健康状况直接决定了服务是否稳定。
本文将介绍如何通过 python 和 shell 脚本快速实现接口监控,自动读取接口列表,批量检测接口返回状态和耗时,并最终生成一份清晰的可视化报告(支持 markdown 或 html 格式)。这样你就能在最短时间内确认接口是否健康,而不是盯着浏览器或 postman 一次次重复操作。
引言
随着微服务、api 网关、容器化部署的普及,一个完整的应用系统可能依赖几十个甚至上百个 rest api 接口。每次上线之后,团队都要花费时间确认接口是否正常响应,尤其是在跨环境(开发、测试、生产)部署时,这种验证工作不可或缺。
传统做法是开发手工用浏览器打开、用 postman 点接口,或者通过 curl 一条条执行。这种方式效率很低,而且不可持续。理想的方式应该是:
- 批量读取接口地址
- 自动检测接口是否存活、返回码和响应时间
- 把结果生成报告,一目了然
接下来,我们就来一步步构建一个这样的轻量化解决方案。
用 python 快速实现接口监控
读取接口列表
通常我们会把接口集中放在一个文本文件里,比如 urls.txt
:
https://api.example.com/health
https://api.example.com/v1/user
https://api.example.com/v1/order
每一行代表一个待检测的接口。
核心脚本实现
下面的 python 脚本使用 requests
来检测接口,记录返回的状态码和耗时,并把结果写入一个 markdown 报告文件。
import requests import time from datetime import datetime def check_url(url): try: start = time.time() response = requests.get(url, timeout=5) duration = round((time.time() - start) * 1000, 2) # 毫秒 return response.status_code, duration except exception as e: return none, none def main(): # 读取接口列表 with open("urls.txt", "r") as f: urls = [line.strip() for line in f if line.strip()] # 生成报告文件 report_name = f"report_{datetime.now().strftime('%y%m%d_%h%m%s')}.md" with open(report_name, "w") as report: report.write("| url | status | response time (ms) |\n") report.write("| --- | ------ | ------------------ |\n") for url in urls: status, duration = check_url(url) if status: report.write(f"| {url} | {status} | {duration} |\n") else: report.write(f"| {url} | error | - |\n") print(f"接口检测完成,结果保存在 {report_name}") if __name__ == "__main__": main()
代码解析
check_url
:负责单个接口检测,返回状态码和耗时。urls.txt
:统一管理所有待检测接口。report_xxx.md
:自动生成 markdown 格式的检测结果,方便阅读或上传到 gitlab/github。
示例输出
| url | status | response time (ms) |
| --- | ------ | ------------------ |
| https://api.example.com/health | 200 | 120.35 |
| https://api.example.com/v1/user | 200 | 220.11 |
| https://api.example.com/v1/order | error | - |
用 shell 脚本实现极简版本
有些时候,python 环境不方便直接部署,这时可以用一个简单的 shell 脚本加 curl
来实现:
#!/bin/bash input="urls.txt" output="report_$(date +%y%m%d_%h%m%s).md" echo "| url | status | time (ms) |" > $output echo "| --- | ------ | --------- |" >> $output while ifs= read -r url do if [ -n "$url" ]; then start=$(date +%s%3n) status=$(curl -o /dev/null -s -w "%{http_code}" "$url") end=$(date +%s%3n) duration=$((end-start)) if [ "$status" -eq 200 ]; then echo "| $url | $status | $duration |" >> $output else echo "| $url | $status | $duration |" >> $output fi fi done < "$input" echo "接口检测完成,结果保存在 $output"
核心逻辑
- 用
curl
获取接口响应状态码。 - 计算时间差作为响应时长。
- 输出 markdown 格式结果。
应用场景举例
场景一:服务上线前的批量验证
开发完成后要上线新版本,可以先在测试环境跑一遍检测脚本,确保所有接口返回 200,再推进到生产。
示例:
python check_api.py
得到报告后,可以快速交给 qa 或运维确认。
场景二:定时健康检查
通过 cron
或 ci/cd 配合脚本定时执行,把接口状态报告每天生成一次。
这样可以快速发现某个接口挂掉的情况,而不是等用户反馈。
示例 crontab -e
配置:
0 * * * * python /home/user/check_api.py
表示每小时执行一次。
场景三:上线后灰度验证
在灰度发布过程中,可以只把部分接口地址放进 urls_gray.txt
,运行检测脚本,快速确认新版本健康。
qa 环节
q1:能不能直接生成 html 报告?
可以。把 markdown 转换成 html 非常容易,python 中可以用 markdown
库,或者直接输出 html 表格。
q2:如果接口需要鉴权怎么办?
可以在 requests.get()
或 curl
中加上 header,比如带上 token。
q3:能不能同时检测并发请求?
完全可以,用 python 的 concurrent.futures
或者 asyncio
实现异步检测,可以大幅度提升检测速度。
总结
本文展示了如何用 python 和 shell 脚本快速实现一套接口监控方案,解决了部署后需要手动逐一验证接口的问题。方案的关键点在于:
- 接口集中管理,批量检测。
- 检测结果可视化(markdown/html)。
- 可结合 ci/cd 或定时任务,实现自动化。
这种方案非常适合微服务架构下的日常运维,既轻量又实用,不需要额外引入复杂的监控平台,就能在第一时间发现接口问题。
到此这篇关于python和shell脚本实现轻量级监控接口的文章就介绍到这了,更多相关python监控接口内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论