我需要监控某个网站在收到地址后需要多长时间才能做出响应。我想嗅探端口 80 上的流量,但只有在与目标站点交换流量时才可以。我已经搜索过了,看起来 pcapy 或 scapy 是完成这项工作的正确工具,但它们似乎比我需要的更深入。我正在研究以下脚本:
Network traffic monitor with pcapy in python
我想我需要改变
def __handle_packet(self, header, data):
# method is called for each packet by dispatch call (pcapy)
self._dispatch_bytes_sum += header.getlen() #header.getlen() #len(data)
logger.debug("header: (len:{0}, caplen:{1}, ts:{2}), d:{3}".format(header.getlen(), header.getcaplen(), header.getts(), len(data)))
#self.dumper.dump(header, data)
以某种方式只解压/处理发往目标站点的数据包。请注意,这是针对 LAN 上的 Windows XP 计算机,浏览器启动流量至关重要。
有什么建议吗?
最佳答案
scapy 的问题是它不处理重组 TCP streams .您要查找的 HTTP 很可能嵌入在 TCP 流中。引用文档:
Scapy is based on a stimulus/response model. This model does not work well for a TCP stack. On the other hand, quite often, the TCP stream is used as a tube to exchange messages that are stimulus/response-based.
就像你说的,scapy 更适合底层的东西。例如,您可能会根据 DHCP 请求跟踪 IP 数据包。与许多网络工具一样,TCP 的复杂性和基于流的特性意味着一旦您跨过该层,就很难重新组装所有内容并处理所有重新传输以及边缘情况以外的情况以及连贯地提取数据。
您能否使用 curl 或 urllib 之类的工具,看看需要多长时间才能返回响应?
关于python - 使用 pcapy 或 scapy 监控自生(HTTP)网络流量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12009442/