用pyshark计算数据包数量 [英] Count the number of packets with pyshark

查看:522
本文介绍了用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)

ilen(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆