以下代码在使用以下advice时给出了不可预测的结果:
import pyshark
import pandas as pd
import asyncio
def ProcessPackets(packet):
global packet_list
packet_version = packet.layers[1].version
layer_name = packet.layers[2].layer_name
packet_list.append([packet_version, layer_name, packet.length, packet.sniff_time])
def Capture(timeOrPath):
global packet_list
packet_list=[]
try:
timeout=int(timeOrPath)
capture = pyshark.LiveCapture()
capture.apply_on_packets(ProcessPackets, timeout=timeout)
except asyncio.TimeoutError:
pass
except ValueError:
capture = pyshark.FileCapture(timeOrPath)
capture.load_packets()
capture.apply_on_packets(ProcessPackets)
data = pd.DataFrame(packet_list, columns=['vIP', 'protocol', 'length','timestamp'])
print(data['timestamp'].iloc[-1]-data['timestamp'].iloc[0])
def main():
Capture(6)
if __name__ == '__main__':
main()
有时计算的时间超过给定的超时时间。
(timestamp
是packet.sniff_time
)
更新日期:06-03-2021
在对这个捕获延迟问题进行了一些研究之后,我确定问题可能与等待加载dumpcap有关dumpcap以LiveCapture模式加载
上面的代码将在我的系统上启动此功能:
这是:
我尝试将一些自定义参数传递给LiveCapture
但是仍然有大约1/2秒的延迟
在dumpcap文档中有一个-a模式,它允许持续时间超时,但我无法将该参数传递到pyshark,而不会导致错误
Tshark也有一个-a模式,但它也会在pyshark中导致错误
可能有办法修改pyshark代码库中的超时参数,以允许-a模式。要做到这一点需要一些测试,而我目前没有时间做
我与pyshark的开发人员一起打开了一个issue on this problem
原邮政编码:06-02-2021
我修改了您的代码,将提取的项写入数据帧中。如果这不是您想要的,请根据您的具体要求更新您的问题
相关问题 更多 >
编程相关推荐