Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在后台运行pyshark_Python_Multithreading_Background_Tshark_Pyshark - Fatal编程技术网

Python 在后台运行pyshark

Python 在后台运行pyshark,python,multithreading,background,tshark,pyshark,Python,Multithreading,Background,Tshark,Pyshark,我想在后台运行pyshark,这样在它运行时,我仍然能够执行一些web操作并捕获它们。 一个强制性条件是,我必须能够使用tshark进行解析,因为我有一些专有的Wireshark解析器 基本上我需要做的是: 启动网络捕获 执行一些web操作 停止捕获(或等待停止条件) 迭代捕获对象并检查每个数据包的属性 我不能按原样使用capture.sniff(),因为它在阻塞模式下工作,也不能连续使用capture.sniff\u(),因为它是一个生成器 问题: 我尝试从线程调用sniff(),然后等待它以

我想在后台运行pyshark,这样在它运行时,我仍然能够执行一些web操作并捕获它们。 一个强制性条件是,我必须能够使用tshark进行解析,因为我有一些专有的Wireshark解析器

基本上我需要做的是:

  • 启动网络捕获
  • 执行一些web操作
  • 停止捕获(或等待停止条件)
  • 迭代捕获对象并检查每个数据包的属性
  • 我不能按原样使用capture.sniff(),因为它在阻塞模式下工作,也不能连续使用capture.sniff\u(),因为它是一个生成器

    问题: 我尝试从线程调用sniff(),然后等待它以join()结束。 但是当我到达迭代器时,tshark.exe会重新启动并覆盖捕获文件:

    print('Background sniffing:')
    capture = pyshark.LiveCapture(interface='Ethernet', bpf_filter='host 10.20.30.40', output_file='bg_capture.pcapng')
    t = threading.Thread(target=capture.sniff, kwargs={'timeout': 30, 'packet_count': 5000}, daemon=True)
    t.start()
    print('Do some stuff web action here...')
    t.join()
    print('Done sniffing')
    for p in capture: # At this point, tshark re-launch itself
        print(f'Packet number {p.number}')
    

    你找到解决方案了吗?不幸的是,没有。我正在考虑在后台进程中打开tshark,捕获到一个文件。然后用pyshark打开文件并使用它的解析功能。我找到了解决方案。只需使用dumpcap将流量记录到一个文件中,然后使用pyshark打开该文件。