1

Im new to python and I'm testing the library pyshark.

I want to sniff the traffic all the time and do other things. But when I start the sniffer, all other code stop working. I've tried asyincio thread and multithreading, but it always give me different errors.

This the code that I want to run without blocking the application.

import pyshark
import asyncio

class Sniffer:

    @staticmethod
    @asyncio.coroutine
    def sniff():
        cap = pyshark.LiveCapture(interface='en0')
        cap.sniff_continuously(packet_count=0)
        for pkt in cap:
            print(pkt)

Thanks in advice.

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Arturo
  • 63
  • 7

1 Answers1

0

You can try multi-threading class. https://docs.python.org/3/library/threading.html

For example :

import thread
obj = Sniffer()
thread.start_new_thread( obj.sniff,)

Another way if you tired to multi threading or someone else, create new python file and import your sniffer.

For example :

# new python file
import sniffer
obj = sniffer.Sniffer()
while True:
    obj.sniff()

Thi way use your processor cores. This is as multi thread with little difference, your operating system make this work automatically.

Talha Çelik
  • 171
  • 3
  • 7
  • Pyshark maybe blocking your ethernet card. Try another sniffer method or library. Another way is create virtual ethernet card and make a bridge between real device with virtual device then connect virtual card for sniffing. **cap = pyshark.LiveCapture(interface='my_virtual_eth')** – Talha Çelik Jun 01 '20 at 08:52