希望这会有所帮助,捕获超时为 1 秒的数据包并检索它们
import pyshark
capture = pyshark.LiveCapture(interface=r'\Device\NPF_{D41D8EE1-2739-4FA1-8873-024D3F68E9E1}',
output_file=r'C:\Temp\samp1.pcap')
capture.sniff(timeout=1)
pkts = [pkt for pkt in capture._packets]
print(len(capture))
capture.close()
但是使用 capture.close() 似乎有一些 asyncio 异常。无论如何,这不会影响我们的代码。输出如下
94
taking long time to close proactor
Task exception was never retrieved
future: <Task finished coro=<_close_async() done, defined at C:\Python34\lib\site-packages\pyshark\capture\capture.py:409> exception=RuntimeError('Set changed size during iteration',)>
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\trollius\tasks.py", line 255, in _step
result = next(coro)
File "C:\Python34\lib\site-packages\pyshark\capture\capture.py", line 411, in _close_async
for process in self.running_processes:
RuntimeError: Set changed size during iteration
Task was destroyed but it is pending!
task: <Task pending coro=<packets_from_tshark() running at C:\Python34\lib\site-packages\pyshark\capture\capture.py:261> wait_for=<Task finished coro=<_close_async() done, defined at C:\Python34\lib\site-packages\pyshark\capture\capture.py:409> exception=RuntimeError('Set changed size during iteration',)>>
Process finished with exit code 0