Utility to pack and unpack binary data into commonly used python data types.
Project description
packetutil
packetutil is a Python utility library for defining and manipulating custom binary packet structures using flexible field definitions. It makes it easy to pack Python dictionaries into bytearrays and unpack bytearrays back into structured data — with support for bitfields, bytearrays, and typed fields.
Features
- Define binary packet formats with intuitive field objects
- Convert Python dictionaries to binary with
pack() - Decode binary into dictionaries with
unpack() - Supports:
- Typed fields (
uint8,uint16, etc.) - Fixed-size byte arrays
- Bitfields with named subfields and bit lengths
- Typed fields (
Installation
pip install packetutil
Usage
Define your packet structure
from packetutil import PacketFormatter, TypeField, BytearrayField, BitField
fields = {
'type_f': TypeField('uint16', big_endian=False),
'byte_f': BytearrayField(4),
'bit_f': BitField('uint16', {
'field1': 3,
'field2': 6,
'field3': 7
})
}
formatter = PacketFormatter(fields)
Pack data into a bytearray
data = {
'type_f': 1,
'byte_f': bytearray(4 * [0]),
'bit_f': {
'field1': 7,
'field2': 20,
'field3': 30
}
}
packed = formatter.pack(data)
print(packed)
# Output:
# bytearray([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEA, 0x1E])
Unpack a bytearray into structured data
parsed = formatter.unpack(bytearray([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEA, 0x1E]))
print(parsed)
# Output:
# {
# 'type_f': 1,
# 'byte_f': bytearray([0, 0, 0, 0]),
# 'bit_f': {
# 'field1': 7,
# 'field2': 20,
# 'field3': 30
# }
# }
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file packetutil-0.1.2.tar.gz.
File metadata
- Download URL: packetutil-0.1.2.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfd7d15dc8afd5d7497381eb0bee5657532c20dcc8691e01e0ad76b55b4bc8f8
|
|
| MD5 |
d7d13f4fdf33a45775a3b56a53ffcf7e
|
|
| BLAKE2b-256 |
fbabae7d6ffd540480ede8b0ef9db58de541c2ec286505bb94af4894e0c2055f
|
File details
Details for the file packetutil-0.1.2-py3-none-any.whl.
File metadata
- Download URL: packetutil-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
476157e9f949ba389cb127406a62228215750069032510583f211164506e00a5
|
|
| MD5 |
6223fd22817a15d3618f463c5b7b6f42
|
|
| BLAKE2b-256 |
704b4b233295f73ebc6c85ea4d3da7ae5b9d4d0c96bd919889f81890d0183658
|