Public API¶
A synchronous client for the LocalSend v2 protocol.
Use Client to discover and register peers, send and receive files, and
share files with a browser.
- class localsend.Client(name: str | None = None, *, download_directory: Path | str | None = None, on_receive_request: Callable[[IncomingTransfer], bool | Collection[str] | None] | None = None, on_received: Callable[[ReceivedFile], None] | None = None, on_receive_failed: Callable[[ReceiveFailure], None] | None = None, on_share_request: Callable[[ShareRequest], bool] | None = None, port: int = 53317, advertise_host: str = '127.0.0.1', device_model: str | None = None, device_type: str | None = 'desktop', pin: str | None = None, session_timeout: float = 300.0, identity_path: Path | str | None = None, discovery_interfaces: Iterable[str] | None = None, http_discovery_networks: Iterable[IPv4Network] = ())¶
Discover LocalSend peers and transfer files synchronously.
Omit
nameto generate an English adjective-and-noun alias.device_modelanddevice_typeidentify this client to peers.To receive files, set
download_directoryand provideon_receive_request. It runs on an incoming-request worker and may returnFalseto reject all files, a collection of file IDs to accept a subset, orTrue/Noneto accept all files. Concurrent offers may invoke it concurrently.on_receivedruns serially after each file is saved; callback failures are logged, andclose()waits for queued callbacks.on_receive_failedreports accepted files that cannot be saved completely.on_share_requestauthorizes browser downloads and runs synchronously on an incoming-request worker. ReturnTrueto authorize the browser-bound session. Setadvertise_hostto this machine’s LAN address before sharing files with another device.discovery_interfacesselects IPv4 addresses for multicast; otherwise locally resolved addresses are used.http_discovery_networksenables a bounded private-IPv4 scan when multicast is unavailable. Interfaces are selected bystart(); after an interface change or multicast-listener error, callclose()andstart().The client keeps a mutual-TLS identity at
$XDG_STATE_HOME/localsend-python/identity.pemor~/.local/state/localsend-python/identity.pem. Setidentity_pathto retain it elsewhere or rotate it.- property devices: tuple[Device, ...]¶
Return LocalSend devices found through discovery or registration.
- register(peer: Device) Device¶
Register with
peerand return its confirmed identity.Uses the peer’s advertised HTTP or HTTPS protocol and pins an HTTPS certificate to its discovered fingerprint. The confirmed peer is added to
devices. Network and invalid-response failures raiseRuntimeError.
- info(peer: Device) DeviceInfo¶
Return diagnostic identity information from
peer.Pins an HTTPS certificate to the peer’s discovered fingerprint. Network and invalid-response failures raise
RuntimeError.
- start() None¶
Start the HTTP server and multicast discovery socket.
Concurrent calls share one startup transition. A client closed previously is reopened before its listeners start.
- close() None¶
Stop the HTTP server and multicast discovery socket.
This serializes with
start(); it is safe to call repeatedly.
- prepare_upload(peer: Device, files: Iterable[Path | str], *, pin: str | None = None) UploadSession | None¶
Offer
filestopeerand return its accepted upload session.Call
UploadSession.upload()to send accepted files orUploadSession.cancel()to withdraw the offer. ReturnsNoneiffilesis empty or the peer accepts no files; an empty iterable makes no network request. A peer may accept only a subset of the offer.Pins an HTTPS certificate to the peer’s discovered fingerprint. Missing or invalid PINs raise
PinRequiredError; a rejected offer raisesUploadRejectedError; an active peer transfer raisesUploadSessionBlockedError; and network failures raiseRuntimeError. Connection establishment is limited to two seconds, then the receiver has five minutes to approve the offer.
- send_files(peer: Device, files: Iterable[Path | str], *, pin: str | None = None) tuple[Path, ...]¶
Send
filestopeerand return the uploaded paths.Equivalent to
prepare_upload()followed byUploadSession.upload(). A peer may partially accept an offer, so the result contains only accepted, successfully uploaded paths. An empty iterable returns()without a network request.If the client is started and the peer cancels an active upload,
UploadCancelledErroris raised. Network failures raiseRuntimeError.
Publish
filesat LocalSend’s browser-download endpoint.Start the client first so the returned URL is reachable. Set
advertise_hostto this machine’s LAN address before sharing with another device.
- class localsend.Device(alias: str, fingerprint: str, host: str | None = None, port: int = 53317, protocol: str = 'http')¶
A LocalSend device visible to an incoming transfer.
- class localsend.DeviceInfo(alias: str, version: str, fingerprint: str, device_model: str | None, device_type: str | None, download: bool)¶
Diagnostic identity information reported by a LocalSend peer.
- class localsend.IncomingFile(id: str, name: str, size: int, media_type: str)¶
Metadata for one file offered by a remote sender.
- class localsend.IncomingTransfer(sender: Device, files: tuple[IncomingFile, ...])¶
An incoming transfer offered for local authorization.
- class localsend.ReceiveFailure(sender: Device, file: IncomingFile, received_size: int, reason: Literal['size_mismatch', 'write_failed'])¶
An incoming file that could not be saved completely.
- class localsend.ReceivedFile(sender: Device, file: IncomingFile, path: Path)¶
A file saved after an incoming transfer was authorized.
A browser asking to download the files in the current share.
- exception localsend.PinRequiredError¶
Raised when a peer requires a missing or different upload PIN.
- exception localsend.UploadCancelledError¶
Raised when a peer cancels an active outbound upload.
- exception localsend.UploadRejectedError¶
Raised when a peer rejects an offered upload before file transfer.
- class localsend.UploadSession¶
An accepted outbound upload that can be sent or cancelled once.
- property accepted_files: tuple[Path, ...]¶
Return the offered paths accepted by the receiving peer.
- cancel() None¶
Cancel this upload session at the receiving peer.
- upload() tuple[Path, ...]¶
Upload the accepted files to the receiving peer.
- exception localsend.UploadSessionBlockedError¶
Raised when a peer has another active upload session.