Package: packeteer
Packeteer: The packet serializerPacketeeris a light-weight packet serializer capable of translating between raw bytes and custom packet objects; Objects which are easier to understand, display, and work withimportsocketfrompacketeerimportpackets,fieldsHOST='127.0.0.1'PORT='1234'classRequest(packets.BigEndian):""" Request packet """fields=[fields.UInt8('type'),fields.UInt8('size'),fields.Raw('data',size='size')]classResponse(packets.BigEndian):""" Response packet """fields=[fields.Bool('success'),fields.Uint32('transfered'),]if__name__=='__main__':data=b'Hello World'request=RequestPacket(type=0,data=data)response=ResponsePacket()withsocket.socket(socket.AF_INET,socket.SOCK_STREAM)ass:s.connect()s.sendall(request)raw=s.recv(response.size())response.unpack(raw)print(repr(response))# <Packet: Response packet>#   success: True#   transfered: 11InstallationPacketeeris available on PyPi, and can be installed with pip directly$pipinstallpacketeerRequirementsOperating systemsPacketeerhas no OS dependencies, and should be compatible wherever python can run; However, it is only verified for Ubuntu 18.04. If you discover any issues in other environments, please open a new issue or submit a pull request.PythonPacketeerwas developed and tested against python 2.7, 3.5, 3.6, and 3.7.DependenciesPacketeerdepends on the following packages:futuresixFor development and testing, these optional dependencies are also required:pytestpytest-covtoxDocumentationDefining packetsDefining packets is as simple as deriving a new class from eitherpackets.BigEndianorpackets.LittleEndian(Depending on the byte ordering of your packet structure)importcopyfrompacketeerimportpackets,fieldsclassMyPacketBE(packets.BigEndian):name='Custom (Big Endian)'fields=[fields.Bool('OK'),fields.Int32('value',default=42)]classMyPacketLE(packets.LittleEndian):""" Custom (Little Endian) """fields=copy.deepcopy(MyPacketBE.fields)Take notice that the packet definition is built using thefieldsvariable, and the built-in field types; Field types and there options are explained in the fields section of the documentation.The packet name is an optional value that can be set directly, otherwise it will be coppied from the classes doc-string. If no name is found, the name will be given a default value. The name is only used for human readibility when using repr()Working with packetsCreating new instancesWhen packet objects are constructed without any parameters, their default values are stored in each fieldfrompacketeerimportpackets,fieldsclassMyPacket(packets.BigEndian):""" Custom (Big Endian) """fields=[fields.Bool('OK'),fields.Int32('value',default=42)]packet=MyPacket()print(repr(packet))# <Packet: Custom (Big Endian)>#   OK: False#   value: 42Packets can also be constructed with non-default values by using the field names to set valuespacket=MyPacket(OK=True)print(repr(packet))# <Packet: Custom (Big Endian)>#   OK: True#   value: 42packet2=MyPacket(value=100)print(repr(packet2))# <Packet: Custom (Big Endian)>#   OK: False#   value: 100Working with packet valuesField values can be accessed like a list (By index) or like a dictionary (By key)packet=MyPacket()print(packet[0],packet[1])# False 42print(packet['OK'],packet['value'])# False 42Values can also be set in the same fashionpacket=MyPacket()packet[0]=Truepacket[1]=100print(packet['OK'],packet['value'])# True 100Packing/UnpackingThe entire purpose of this library is to work with bytes, so it should come to no surprise that packet instances can be serialized into their raw bytes and back.packet=MyPacket()raw=b'\x01\x00\x00\x00\xFF'packet.unpack(raw)print(repr(packet))# <Packet: Custom (Big Endian)>#   OK: True#   value: 255raw2=packet.pack()print(raw==raw2)# TruePacket instances can be constructed directly from bytes as well using the from_raw() callpacket=MyPacket.from_raw(b'\x01\x00\x00\x00\xFF')print(repr(packet))# <Packet: Custom (Big Endian)>#   OK: True#   value: 255FieldsThe different components of the packet are referred to as fields, which are a collection of the associated value, meta data, and supporting functions.Packeteer comes with the following field types:fields.Padding: (1 Byte) N/Afields.Bool: (1 Byte) Booleanfields.Char: (1 Byte) Characterfields.Int8: (1 Byte) Signed Integerfields.UInt8: (1 Byte) Unsigned Integerfields.Int16: (2 Byte) Signed Integerfields.UInt16: (2 Byte) Unsigned Integerfields.Int32: (4 Byte) Signed Integerfields.UInt32: (4 Byte) Unsigned Integerfields.Int64: (8 Byte) Signed Integerfields.UInt64: (8 Byte) Unsigned Integerfields.Float: (4 Byte) Float valuefields.Double(8 Byte) Float valuefields.Raw: (n Byte) Raw byte data as a single valuefields.String: (n Bytes) Unicode String as a single valueThe majority of the types are self explanatory and work identically to the others, but some like padding, string, and raw behave differently and are looked at further in the following sectionsPaddingfields.Paddingis a special field type that is 1 byte wide per character.Padding bytes are nameless and not associated with any value; They can't be accessed, but they are counted when packing and unpacking.frompacketeerimportpackets,fieldsclassPaddedPacket(packets.BigEndian):""" Padded """fields=[fields.Padding(),fields.UInt8('value'),fields.Padding(default=b'\xff')]packet=PaddedPacket(value=170)print(repr(packet))# <Packet: Padded>#   value: 170print(packet[0])# 170print(packet[1])# IndexErrorraw=packet.pack()print(repr(raw))# '\x00\xAA\xFF'packet.unpack(b'\x00\x7f\xff')print(repr(packet))# <Packet: Padded>#   value: 127Raw data and stringsfields.Rawis a raw byte store of a given size (The size argument is required). If the data is too large for the field, it will be truncated to fit. Likewise if it is too short, it will be padded with null bytes.fields.Stringis an extension offields.Rawthat stores it's internal value as a unicode string with the encoding of your choosing (Defaults to utf8). The internal value has any trailing null byte padding removed until it is serialized.frompacketeerimportpackets,fieldsclassDataPacket(packets.BigEndian):""" Data Packet """fields=[fields.Raw('raw',size=12),fields.String('string',size=12,encoding='utf8')]packet=DataPacket(raw=b'Hello World',string='Hello World')print(repr(packet))# <Packet: Raw Packet>#   raw: b'Hello World\x00'#   string: u'Hellow World'List fieldsThere are often times when you need to have a variable list of values in a packet (Think about a repeating set of values depending on a given count value).fields.Listtakes care of this.fields.Listrequires an additional argument of the field the list contains, with the rest of the arguments given as keywords that the underlying field type requires.frompacketeerimportpackets,fieldsclassListPacket(packets.BigEndian):""" List Packet """fields=[fields.UInt8('count')fields.List('messages',fields.String,size=128),]messages=['foo','bar','Hello World']packet=ListPacket(count=len(messages),messages=messages)print(repr(packet))# <Packet: List Packet>#   count: 3#   messages: [u'foo', u'bar', u'Hello World']