Search code examples
pythonpython-2.7pyshark

pyshark packets queue and pickle errors


I am trying to use pyshark to capture packets live.

I get the following error when I try to get the packets from a multiprocessing.Queue or to un-pickle them:

python2.7/site-packages/pyshark/packet/layer.py", line 48, in __getattr__
val = self.get_field_value(item, raw=self.raw_mode)
(... multiple times ...)
RuntimeError: maximum recursion depth exceeded while calling a Python object`.

I suspect there is a problem when reconstructing the object whether it's retreived from the queue, or unpickled.
Surprisingly though, there are no errors when I am doing this with Queue.Queue.

Here is the code used to reproduce this issue :

import pyshark
import multiprocessing
import Queue
import cPickle as pickle

# Capture on eth0
interface = pyshark.LiveCapture(interface="eth0")

def queue_test(queue):
    """ Puts captured packets in a queue, then un-queue them and display """
    for packet in interface.sniff_continuously(packet_count=5):
        queue.put(packet)
    while not queue.empty():
        packet = queue.get()
        print "Packet {} {}".format(packet.highest_layer,packet._packet_string)

def pickle_test():
    """ Immediately pickle and unpickle the packet to display it"""
    for packet in interface.sniff_continuously(packet_count=5):
        pickled_packet = pickle.loads(pickle.dumps(packet, pickle.HIGHEST_PROTOCOL))
        print "Packet #{}, {} {}".format(pickled_packet.highest_layer,pickled_packet._packet_string)


if __name__ == "__main__":
    normal_queue = Queue.Queue()
    process_queue = multiprocessing.Queue()

    # Runs fine
    queue_test(normal_queue)

    # Both crash with a RuntimeError
    queue_test(process_queue)
    pickle_test()

Why do I get RuntimeErrors and what can I do about it?
Am I doing something wrong or pyshark has a problem doing this?


Solution

  • Having not much success here, I posted an issue on pyshark's Github and it happens that it was something missing in the library:

    This is caused by the fact that some of the classes packet uses override getattr. Fixed in 541fc52

    Link of this issue : https://github.com/KimiNewt/pyshark/issues/63