用pyshark计算数据包数量 [英] Count the number of packets with pyshark
问题描述
在此代码中使用pyshark
In this code with pyshark
import pyshark
cap = pyshark.FileCapture(filename)
i = 0
for idx, packet in enumerate(cap):
i += 1
print i
print len(cap._packets)
i
和len(cap._packets)
给出两个不同的结果.为什么会这样?
i
and len(cap._packets)
give two different results. Why is that?
推荐答案
A look at the source code reveals that _packets
is a list containing packets and is only used internally:
使用keep_packets = True
数据包遍历FileCapture
对象时,会将其添加到此列表中.
When iterating through a FileCapture
object with keep_packets = True
packets are getting added to this list.
要访问FileCapture
对象中的所有数据包,您应该像以前那样遍历它:
To get access to all packets in a FileCapture
object you should iterate over it just like you did:
for packet in cap:
do_something(packet)
但是要计算数据包的数量,只需执行以下操作:
But to count the amount of packets just do this:
packet_amount = len(cap)
或像您一样使用计数器,但是请勿使用 _packets
,因为它不包含完整的数据包列表.
or use a counter like you did, but don't use _packets
as it does not contain the complete packet list.
这篇关于用pyshark计算数据包数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!