Repository navigation
Quick start
Easiest: Host and Device does pairing (by QR code), transport, discovery and storage in a few lines. This page shows the building blocks underneath, for when you want your own transport or pairing flow.
pip install "lanpair[keyring,discovery]"The extras are optional: keyring keeps keys in the OS key store, discovery finds
devices with mDNS. The core (pip install lanpair) needs only cryptography.
from lanpair import load_or_create_identity
me = load_or_create_identity() # generated on first run, the same one after a restart
print(me.public.device_id) # a short, stable id, e.g. "4f1c0a9e7b2d1c33"The 32-byte seed goes to the macOS Keychain, Windows Credential Manager or the Secret
Service on Linux. Without the keyring extra, use Identity.generate() and store
identity.seed() somewhere safe yourself.
Each side needs the other's public identity (64 bytes). It is not secret, but it must be the real one: get it over a channel an attacker on the LAN cannot change, such as a QR code shown on one screen and scanned by the other device, or copied by hand.
import base64
from lanpair import PublicIdentity
text = base64.b64encode(me.public.to_bytes()).decode() # show this as a QR code or text
# on the other device:
peer = PublicIdentity.from_bytes(base64.b64decode(text))Built-in QR and 6-digit-code pairing are on the Roadmap.
from lanpair import JsonFileSeqStore, Opener, Sealer
# sender
wire = Sealer(me).seal(peer, {"type": "close"}) # bytes: send them any way you like
# receiver
inbox = Opener(me, store=JsonFileSeqStore("seqs.json"))
inbox.add_peer(peer)
sender, body = inbox.open(wire) # or EnvelopeErrorlanpair does not open sockets for messages: send the bytes over UDP, TCP, WebSocket, a serial line, anything. Recipe: commands over UDP shows a complete example.
from lanpair.discovery import Announcer, find
with Announcer(me.public, "Room 3", port=8765): # on the receiving device
...
room = find(peer.device_id) # on the sender: FoundDevice or NoneDiscovery only tells you where a device says it is. It never decides trust: a fake announcement can at most send your message to the wrong address, where it is not delivered, but also cannot be read or used.
lanpair is MIT-licensed. Found a mistake on this page? Open an issue.