我正在尝试创建一个 Python 脚本,在其中解析 pcap 文件并测量低至亚毫秒级别的流量突发性。问题是 dpkt 提供的时间戳似乎无法提供足够精细的分辨率。以下脚本 #!/usr/bin/python 导入dpkt,系统
f=file(sys.argv[1],"rb")
pcap=dpkt.pcap.Reader(f)
for ts, buf in pcap:
eth=dpkt.ethernet.Ethernet(buf)
size = len(eth)
print "packet size %s" % size
print "timestamp %s" % ts
产生以下结果
数据包大小 199 时间戳 1397589057.04 数据包大小 119 时间戳 1397589057.04 数据包大小 66 时间戳 1397589057.04 数据包大小 247 时间戳1397589057.04
这些数据包的实际时间戳应采用 .043549 格式,精确到微秒。有谁知道如何获得完整的时间戳?
最佳答案
我假设你正在做这样的事情:
for ts, buf in pcap:
print ts
然后您观察到时间戳为 1408173480.93
而不是 1408173480.936543
。这是因为Python中的print函数将float限制为小数点后两位。
示例:
>>> x = 1258494066.119061
>>> x
1258494066.119061
>>> print x
1258494066.12
如果您确实需要打印完整值,请使用格式:
>>> "{0:.6f}".format(x)
'1258494066.119061'
关于python - 使用 dpkt 的微秒分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23093447/