0

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?

I.K.
  • 414
  • 6
  • 18
  • 1
    Can you try leaving out the last except statement? Do you get an error, if there is no packet and the statement is commented out? – NewEyes Apr 11 '19 at 14:15
  • Thank you so much for interest. I tried removing the try except block and just calling packet = next(packet_generator). And a print statement afterwards. I got no exception error and the text inside the print is NOT printed. – I.K. Apr 11 '19 at 14:32
  • Looking at your desired result you do currently expect an error if there is no packet availabe (as thats the only case when the except statement gets executed). The easiest way to fix this is probably to just back away from this and write another if statement – NewEyes Apr 11 '19 at 14:49

0 Answers0