Python小工具之httpstat网络分析

一、简介

Python httpstat是一个基于Python的命令行工具,用于测量HTTP请求的性能和状态信息。它能够向目标服务器发送HTTP请求,并显示详细的统计信息,包括DNS解析时间、建立连接时间、TLS/SSL握手时间、首字节时间、总时间等。这些信息对于排查网络问题、优化Web应用程序以及监控HTTP请求的性能非常有帮助。

httpstat通过封装curl命令,将整个连接过程每个阶段耗时可视化统计出来,就如README所述:"httpstat visualizes curl(1) statistics in a way of beauty and clarity。"

主要功能:

显示HTTP请求的详细性能统计信息。

支持HTTP和HTTPS协议。

提供对不同阶段的时间度量,如DNS解析、连接建立、TLS/SSL握手等。

支持自定义HTTP请求头和参数。

支持跟踪重定向。

支持IPv6。

二、安装使用

1、离线下载安装

wget https://raw.githubusercontent.com/reorx/httpstat/master/httpstat.py

mv httpstat.py /usr/bin/httpstat

chmod +x /usrbin/httpstat

2、在线pip安装

pip install httpstat

3、使用案例

httpstat https://www.baidu.com

httpstat将向指定URL发送HTTP请求,并显示详细的性能统计信息,如DNS解析时间、连接建立时间、TLS/SSL握手时间、首字节时间、总时间等。

从这个测试来看,dns这个环节耗时较长,指定新的dns服务器后,再去监测,明显响应时间得到提升。

三、展开使用

在实际应用中,可能需要定期监测你的网站或Web应用程序的性能。使用Python httpstat,可以编写一个脚本,定期测试关键URL,并将性能数据记录下来,以便进行性能分析和长期趋势分析。

python 复制代码
import subprocess
import time

# 要监控的URL列表
urls = ["https://www.example.com", "https://www.example2.com"]

while True:
    for url in urls:
        # 运行httpstat命令并捕获输出
        command = f"httpstat {url}"
        result = subprocess.run(command, shell=True, capture_output=True, text=True)

        # 将性能数据记录到日志文件
        with open("performance.log", "a") as log_file:
            log_file.write(result.stdout)

    # 每隔一段时间执行一次测试
    time.sleep(3600)  # 每小时执行一次
相关推荐
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh2 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅2 天前
Python函数入门详解(定义+调用+参数)
python
曲幽2 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时2 天前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿2 天前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780512 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng82 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi3 天前
Chapter 2 - Python中的变量和简单的数据类型
python