Skip to main content

magpie-bridge (Python)

License: MIT Python

Reliable UDP Relay Network — wire protocol library for Python.

This package implements the Magpie Bridge protocol codec: packet packing, unpacking, validation, message segmentation, and acknowledgement helpers.

中文文档

Installation

pip install magpie-bridge

Or install from source:

git clone https://github.com/moky/magpie-bridge.git
cd magpie-bridge/python
pip install -e .

Quick Start

from magpie_bridge import Packet, split_message, build_ack, TYPE_MINI

# Build and send a small packet
pkt = Packet(TYPE_MINI, target_bid=42, source_bid=7, sn=1, body=b"hello")
data = pkt.pack()              # bytes ready for UDP send

# Parse a received packet
received = Packet.unpack(data)
print(received.body)           # b"hello"

# Split a large message into segments
message = b"x" * 5000
segments = split_message(message, sn=2, source_bid=7, target_bid=42)
for seg in segments:
    send(seg.pack())

# Build an acknowledgement
ack = build_ack(received)
send(ack.pack())               # body=b"OK", bids swapped

Protocol Overview

Field Size Description
Magic Code 4 B MB\x00\x01 (Magpie Bridge v1)
Type 1 B packet type + parameter width
Head Length 1 B 20 / 22 / 24 / 28
Body Length 2 B body size in octets
Target Bridge ID 4 B destination bid
Source Bridge ID 4 B originator bid
Serial Number 4 B message identity
index / count 0/2/4/8 B segment ordinal / total segments
Body 0..1200 B application payload
  • Maximum UDP payload (MSS): 1232 octets (1280 - 40 - 8, dual-stack safe).
  • Maximum body size: 1200 octets.
  • See ../docs/design-protocol.md for the full specification.

API Reference

Packet

Packet(type, target_bid, source_bid, sn, index=None, count=None, body=b"")
  • pack() -> bytes — serialize.
  • Packet.unpack(data: bytes) -> Packet — parse.
  • validate() -> bool — check whether the packet is well-formed.
  • message_id -> int — deduplication key (sn for data packets, (sn << (type & 0x0F)) | index for ACKs).
  • head_length, param_width, is_ack — derived properties.

Helpers

  • select_type(message_size) -> int — choose Type by message size.
  • split_message(data, sn, source_bid, target_bid) -> list[Packet].
  • build_ack(packet) -> Packet — build a b"OK" acknowledgement.
  • build_control(body, sn, source_bid=0, target_bid=0) -> Packet — build a control packet (SYN/ACK/PING/PONG/FIN) with Type 0.

Exceptions

  • ProtocolError — raised on invalid packets or parsing failures.

Development

cd python
pip install -e ".[dev]"   # or: pip install pytest
pytest

License

MIT — see LICENSE.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magpie_bridge-0.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

magpie_bridge-0.1.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file magpie_bridge-0.1.0.tar.gz.

File metadata

  • Download URL: magpie_bridge-0.1.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/68.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3

File hashes

Hashes for magpie_bridge-0.1.0.tar.gz
Algorithm Hash digest
SHA256 04bdc8d3d6c620f1938de68ccc69fc615030ad85915e0af40a80d6e5a9d50fc2
MD5 3d3e0aa54295333551c8bfc05da403f7
BLAKE2b-256 fcca8aafa5050f848c0d93210c629e68938b363afa50b65019c0c2f1f3831086

See more details on using hashes here.

File details

Details for the file magpie_bridge-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: magpie_bridge-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/68.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3

File hashes

Hashes for magpie_bridge-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b292f94f1c2a4270b827816dc46a6fe489fed1d830a2938efe2fed44c23adde
MD5 2b1e95222c84961f145bbbbc79ae5cfb
BLAKE2b-256 33655e391576335790a48ce10f2f9d448c6060a493f19fd467acf3d0fd511176

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page