LwPKT (Python)
Pure-Python encoder/decoder for the LwPKT lightweight packet protocol, mirroring the reference C implementation. Read the full protocol documentation at docs.majerle.eu/projects/lwpkt.
Install
pip install lwpkt
Usage
from lwpkt import LwPKT
pkt = LwPKT()
pkt.our_addr = 0x01
pkt.on_error = lambda err: print("Malformed packet, discarded:", err) # optional
# Encode an outgoing packet
data = pkt.generate_packet(b"hello", cmd=0x02, addr_to=0x02)
# Feed received bytes in and process them
pkt.write_rx_data(data)
if pkt.rx_process():
while (received := pkt.rx_get_packet()) is not None:
print(received.data, received.cmd, received.pkt_from)
Like the C library, rx_process() does not stop on malformed input. It drops the packet being
received, records the error and continues with the next byte, so valid packets before and after
a bad one are still decoded. Each error is a typed LwPKTError subclass (LwPKTLengthError,
LwPKTFieldOverflowError, LwPKTCRCError, LwPKTStopByteError):
error_countscounts errors per exception class, e.g.pkt.error_counts[LwPKTCRCError].last_errorholds the most recent one.on_error, if set, is called with each one. If it raises, the exception propagates out ofrx_process(), and the bytes not yet processed are kept for the next call.
The CRC is checked as soon as the CRC bytes arrive, before the stop byte, same as the C library.
rx_get_packet() returns None when no packet is waiting. Use is_for_me(packet) and
is_broadcast(packet) to filter by destination address, and reset() to drop a packet that
is being received.
generate_packet() raises ValueError when a field does not fit its wire encoding
(0..0xFF for non-extended address/command, 0..0xFFFFFFFF otherwise) or the payload is longer
than max_data_len.
Defaults matching the C library
| Attribute | Default | C option |
|---|---|---|
max_data_len |
256 |
LWPKT_CFG_MAX_DATA_LEN (0 = no limit) |
inprog_timeout |
0.1 s |
LWPKT_CFG_PROCESS_INPROG_TIMEOUT (0 = off) |
addr_broadcast |
0xFF |
LWPKT_CFG_ADDR_BROADCAST |
A packet that is still incomplete after inprog_timeout seconds without new bytes is dropped,
so a corrupted length byte cannot block reception. If your application reads the serial port in
batches slower than this, increase the timeout.
Tests
pip install -e .[test]
pytest
tests/c_vectors/vectors.json holds packets encoded by the C library for every option
combination; the tests check the Python encoder produces identical bytes and decodes them.
Regenerate with the command at the top of tests/c_vectors/gen_vectors.c.
License
MIT, see LICENSE.
Metadata
Release files for lwpkt 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| lwpkt-2.1.0.tar.gz | 15.4 kB | With provenance | Details for lwpkt-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| lwpkt-2.1.0-py3-none-any.whl | Python 3 | none | any | Details for lwpkt-2.1.0-py3-none-any.whl |
Total release size: 25.3 kB
Release files / lwpkt-2.1.0.tar.gz
| Download URL | lwpkt-2.1.0.tar.gz |
|---|---|
| Size | 15.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
54142885b88263db9afb2e2d4a5d8272b1049f096979825cf7e8bb4d08640557
|
|
BLAKE2b-256 checksum How to use checksums |
b909a159731d307baf06f5adb0b588c26190d89652dc684ba4a3e1141f3c1a3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / lwpkt-2.1.0-py3-none-any.whl
| Download URL | lwpkt-2.1.0-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
19b8d9f53a72f07525c4db0ef0053f8593163c6536b9e0d1237e2d17a02d0948
|
|
BLAKE2b-256 checksum How to use checksums |
f5e61a6cad953856dbfb87bae919f68ec5b2673925b803b87af408e9aec9fba4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logSecurity for lwpkt 2.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for lwpkt-2.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.