在不同的线程中运行 Pyshark

问题描述

我正在尝试进行一些实时数据包分析。

def live_capture_thread(interface,server_ip,toe_ip,server_cipher=None,bpf_filter='tcp port 443'):

    def _live_capture_thread(interface,bpf_filter='tcp port 443'):
        filtered_cap2 = pyshark.LiveCapture(interface,bpf_filter=bpf_filter)
        tls_packets = filtered_cap2.sniff_continuously() # Generator
        if server_cipher:
            assert check_handshake(tls_packets,server_cipher=server_cipher)
        else:
            assert check_handshake(tls_packets,toe_ip)
    _live_capture_thread(interface,# This is the line I replace.
                         server_cipher=server_cipher,bpf_filter=bpf_filter)

我在主程序中这样调用它:

tshark_thread = threading.Thread(target=live_capture_thread,args=["eth0",manager.ip],daemon=True)
tshark_thread.start()

但是,我使用的是 python3.6 并且在 python 中遇到了一个错误

运行时错误:无法添加子处理程序,子观察者没有附加循环

我尝试自己添加一个事件循环。我添加了以下几行而不是上面代码中对 _live_capture_thread() 的最后一次调用。但是,它似乎没有帮助。

asyncio.set_event_loop(asyncio.new_event_loop())
asyncio.get_event_loop().run_until_complete(_live_capture_thread(interface,server_cipher=server_cipher,bpf_filter=bpf_filter))
asyncio.get_event_loop().run_forever()

我在 github 上遇到了这个悬而未决的问题:https://github.com/KimiNewt/pyshark/issues/353

这基本上表明我需要在主线程上运行 pyshark livecapture 并为我的程序的其余部分分配一个新的事件循环作为解决方法

但是,我想知道我在创建事件循环时是否做错了什么,因为我不是 asyncio 专家。我想在子线程上运行 pyshark,而不是将其留在主线程上。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)