Skip to content

Host and Device

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

Host and Device

Host and Device (lanpair 1.1+) are the high-level API: transport, pairing, discovery, key storage and replay protection in two objects.

pip install "lanpair[all]"   # keyring + discovery + qr

Roles

  • Host: shows the QR code; many devices pair with it (a front desk, a hub, a game host).
  • Device: scans the code; pairs with one host (a room PC, a sensor, a phone).

Both can send and receive.

A complete pair

# host.py
from lanpair import Host

with Host("Front desk") as host:            # start() / close() for you
    if not host.devices:                    # first run only
        offer = host.pairing_offer()        # valid 5 minutes, for one device
        offer.print_qr()                    # or offer.to_uri() / offer.qr().save("pair.png")
        room = host.wait_for_pairing(timeout=300)
        print("paired:", room.name)
        host.send(room.device_id, {"type": "welcome"})
    for msg in host.messages():             # blocks; every message is already verified
        print(msg.sender.name, "says", msg.body)
# device.py
from lanpair import Device

with Device("Room 3") as dev:
    if dev.host is None:                    # first run only
        dev.pair(input("scanned text: "))   # the lanpair://pair?... text from the QR code
    dev.send({"type": "checkout"})
    for msg in dev.messages():
        print("host says", msg.body)

To pair more devices later, call host.pairing_offer() again (for example from a menu). After the first run, both sides remember each other: restart either one and messages keep flowing without pairing again.

Pairing rules

  • An offer carries the host's keys, its address and a one-time secret. Show it only on the host's screen: whoever scans it can pair.
  • One device per offer. It expires after ttl seconds (default 300) and locks after max_attempts wrong answers (default 5). A new pairing_offer() cancels the previous one.
  • dev.pair(...) returns the host once the host confirms, or raises PairingError: expired, for another app, or no confirmation within timeout (default 10 s).

Where things are kept

What Where
Keys OS key store, entry `/<host
Paired peers and their addresses ~/.lanpair/<app>/<role>-<name>/peers.json
Replay counters …/seqs.json
Port …/node.json (reused across restarts)

Pass identity=Identity... and state_dir=... to choose yourself, e.g. on a server without a key store.

Options (both classes)

Option Default
app "lanpair" Your application's name: separate keys, state and mDNS service. Use one
port 0 0 = remember a free port; or a fixed port (open it in the firewall)
bind "0.0.0.0" Listen on all interfaces; give one IP to stay off a VPN or public network
announce True mDNS; with False, devices must reach the address in the QR code
state_dir see above
identity from key store

Receiving

  • for msg in node.messages(): until close().
  • node.receive(timeout=1.0) returns a Message or None.
  • msg.sender is a PeerInfo (device_id, name, identity, address); msg.body is whatever was sent (any JSON value).
  • Anything forged, tampered, replayed, garbage, or from an unpaired device is dropped and counted in node.dropped.

Addresses and restarts

  • Each node reuses its port after a restart.
  • On start, a node says a signed hello to every peer (after an mDNS lookup), so peers learn its current address.
  • Every verified message moves the sender's stored address to where it came from.

Limits

  • Messages up to 1 MiB (1.2+; ValueError above). Small ones go by UDP; ones over about 6 KB go over a short TCP connection to the same port number, so allow both UDP and TCP on that port in a firewall. (1.1 had a limit of about 6 KB.)
  • Delivery is not guaranteed (UDP): if a reply matters, send one and retry on timeout.

Managing devices

for d in host.devices:
    print(d.name, d.device_id, d.address)
host.unpair(device_id)   # nothing from it is accepted any more
dev.unpair()             # the device forgets its host

Clone this wiki locally