Skip to content

UDP recipe

TonyD365-Agent edited this page Nov 21, 2026 · 2 revisions

Recipe: commands over UDP

Since 1.1, Host and Device does all of this, plus QR pairing, for you. This recipe shows the building blocks underneath, for when you need your own transport.

A desk PC sends commands to a room PC. The room listens on UDP and announces itself with mDNS; the desk finds it by device id. Tested with lanpair 1.0.0 on macOS over a real LAN address.

pip install "lanpair[keyring,discovery]"

room.py

# room.py: listens for commands from the paired desk.
import base64
import socket
import sys

from lanpair import EnvelopeError, JsonFileSeqStore, Opener, PublicIdentity, load_or_create_identity
from lanpair.discovery import Announcer

me = load_or_create_identity("room")
print("room public identity:", base64.b64encode(me.public.to_bytes()).decode())

desk = PublicIdentity.from_bytes(base64.b64decode(sys.argv[1]))  # from pairing
inbox = Opener(me, store=JsonFileSeqStore("room-seqs.json"))
inbox.add_peer(desk)

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("0.0.0.0", 0))
port = sock.getsockname()[1]

with Announcer(me.public, "Room 3", port):
    print("listening on port", port, flush=True)
    while True:
        data, addr = sock.recvfrom(65535)
        try:
            peer, command = inbox.open(data)
        except EnvelopeError as e:
            print("dropped packet from", addr, "-", e, flush=True)
            continue
        print("command:", command, flush=True)
        if command.get("type") == "stop":
            break

desk.py

# desk.py: finds the room on the network and sends it commands.
import base64
import socket
import sys

from lanpair import PublicIdentity, Sealer, load_or_create_identity
from lanpair.discovery import find

me = load_or_create_identity("desk")
room = PublicIdentity.from_bytes(base64.b64decode(sys.argv[1]))  # from pairing

found = find(room.device_id, seconds=3)
if found is None:
    sys.exit("room not found on the network")
host = found.addresses[0]
print("found", found.name, "at", found.address)

sealer = Sealer(me)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for command in ({"type": "close", "reason": "checkout"}, {"type": "stop"}):
    sock.sendto(sealer.seal(room, command), (host, found.port))

Run it

Pairing here is done by hand: each script prints or takes the other's public identity as base64. Get the desk's with:

python -c "import base64; from lanpair import load_or_create_identity as l; print(base64.b64encode(l('desk').public.to_bytes()).decode())"

Then:

python room.py <desk public identity>     # on the room PC; prints the room's identity
python desk.py <room public identity>     # on the desk PC

The room prints:

listening on port 64929
command: {'reason': 'checkout', 'type': 'close'}
command: {'type': 'stop'}

What it already protects against

Anyone else on the network can see the packets but not the commands. Sending the same packet again, changing it, sending it to another room, or sending a packet signed with any other key is printed as dropped packet ... - <reason> and never reaches your code.

Going further

  • Keep the room running across restarts: JsonFileSeqStore already remembers what it accepted, and load_or_create_identity keeps the keys.
  • One UDP packet carries up to about 9 KB on macOS by default; for bigger messages use TCP with a length prefix.
  • Replies: give the desk an Opener and the room a Sealer the same way.

Clone this wiki locally