Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of packets with 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 and len(cap._packets) give two different results. Why is that?

like image 940
Bob Avatar asked Dec 02 '22 18:12

Bob


2 Answers

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])

like image 100
Derorrist Avatar answered Jan 21 '23 18:01

Derorrist


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
like image 44
langiac Avatar answered Jan 21 '23 19:01

langiac