我正在尝试编写一个能够计算ping延迟平均值的scapy脚本,所以我需要获取ICMP echo/reply包发送和接收之间的时间。目前我有以下代码:
#! /usr/bin/env python
from scapy.all import *
from time import *
def QoS_ping(host, count=3):
packet = Ether()/IP(dst=host)/ICMP()
t=0.0
for x in range(count):
t1=time()
ans=srp(packet,iface="eth0", verbose=0)
t2=time()
t+=t2-t1
return (t/count)*1000
问题在于使用 time() 函数并不能得到一个好的结果。例如,在一个域名上我发现用 time() 函数得到的时间是 134 毫秒,而使用 ping 系统函数在同一域名上我得到的是 30 毫秒(当然是平均值)。
我的问题是:有没有一种方法可以通过 scapy 得到发送数据包和接收数据包之间的确切时间间隔?我不想使用 popen() 函数或其他系统调用,因为我需要 scapy 来管理未来的数据包。
time.clock()
可能会更加幸运,而不是使用time.time()
。 - Nathan Villaescusasrp1
可能会比srp
更有效。 - Nathan Villaescusa