我有一个从 PyShark 读取 ICMP 数据包的代码块。喜欢 :
capture = pyshark.LiveCapture(interface=self.networkInterface)
packet_generator = capture.sniff_continuously()
while True:
if self.exitProgram:
try:
raise pyshark.capture.capture.StopCapture()
except Exception as e:
print("Stopping ICMP Listener")
break
try:
print("Look if there is Packet")
packet = next(packet_generator)
handlePacket(packet)
except:
print("No Packet")
sleep(0.25)
capture.sniff_continuously
返回生成器的方法。创建 ICMP 数据包时,它会捕获它并发送到handlePacket
,但是当它没有时,我期望输出如下,
Look if there is Packet
No Packet
但是,它只是打印Look if there is Packet
并等待,这是我不想要的。如何让它在给定的循环中运行并检查数据包是否到达?