我将数据包对象转换为dict对象,使我的解析生活更轻松。 代码:
from scapy.all import *
from cStringIO import StringIO
import sys
class Capturing(list):
"""
This class will capture sys.out.
More info:
http://stackoverflow.com/questions/16571150/how-to-capture-stdout-output-from-a-python-function-call
"""
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
sys.stdout = self._stdout
class PacketDict(dict):
"""
This class will convert packet into a dict by using the result of packet.show2(). Furthermore the original
packet will be also saved as attribute '.packet