I have a code block that reads ICMP packets from PyShark. Like :
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)
Method of capture.sniff_continuously
returns a generator. When a ICMP packet is created, it catches it and send to handlePacket
, but when it does not I am expecting an output like,
Look if there is Packet
No Packet
However, it just prints Look if there is Packet
and waits, which is something I do not want. How to keep it running with the given loop and make it check if a packet has arrived or not?