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)  # 每小时执行一次
相关推荐
MeixianAgent43 分钟前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python
咕白m6254 小时前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python
SelectDB21 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
荣码1 天前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
金銀銅鐵2 天前
[Python] 基于欧几里得算法,实现分数约分计算器
python·数学
Lyn_Li2 天前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
小九九的爸爸2 天前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学2 天前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
花酒锄作田3 天前
Pydantic校验配置文件
python
hboot3 天前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络