Count the number of packets with pyshark

家住魔仙堡 提交于 2020-03-22 10:01:36

问题


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 and len(cap._packets) give two different results. Why is that?


回答1:


A look at the source code reveals that _packets is a list containing packets and is only used internally:

When iterating through a FileCapture object with keep_packets = True packets are getting added to this list.


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)

or use a counter like you did, but don't use _packets as it does not contain the complete packet list.




回答2:


Don't know if it works in Python 2.7, but in Python 3.4 len(cap) returns 0. The FileCapture object is a generator, so what worked for me is len([packet for packet in cap])




回答3:


i too, len(cap) is 0, i thinks the answer is fail. if you want know len(cap), please load packet before print it. use: cap.load_packets()

cap.load_packets()
packet_amount = len(cap)
print packet_amount


来源:https://stackoverflow.com/questions/27025827/count-the-number-of-packets-with-pyshark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!