Connectivitizer SDK for Python
huelake-connectivitizer (import connectivitizer): reach a service on a device behind NAT from Python, and run a device in Python. Sessions are encrypted end to end with a key only the device and the application hold; they run peer to peer when the networks allow it and through the Connectivitizer relay when they do not.
It speaks the same protocol as the .NET library, cz, the TypeScript SDK and the ESP32 library, so the two sides of a session can be written in different languages.
import asyncio
from connectivitizer import Client
async def main():
async with Client(api_key="czk_…", shared_key="czs_…") as client:
session = await client.connect("living-room-lamp", ["api"])
response = await session.fetch("api", "/state")
print(response.status, await response.json())
print(session.info.mode) # 'direct' or 'e2e-relay'
print(session.info.end_to_end_encrypted) # established by this endpoint, not claimed by the platform
asyncio.run(main())
Install
pip install huelake-connectivitizer
The guide with complete examples: https://connectivitizer.com/guides/python.md. Python 3.10 or newer; the only dependency is cryptography. Everything else (WebSocket, HTTP, UDP, KCP, the Noise handshake) is in the package and the standard library.
Calling side
Client(api_key=…, shared_key=…) |
Also get_token= (a coroutine function returning a TokenResponse) instead of api_key; disable_direct=True to always use the relay. Use it as async with, or call close(). |
await client.list_devices() |
The devices this application may reach: name, alias, online, and the services it may use. |
await client.connect(device, services) |
Opens a session to a device by its name. |
await session.fetch(service, path, method=, headers=, body=, timeout=) |
An HTTP request to the device's local service: status, headers, await text(), await json(), await bytes(), async for chunk in chunks(). |
await session.websocket(service, path) |
A WebSocket: await send(...), await receive() (None once closed), async for message in socket. |
await session.open_stream(service) |
The raw byte stream (read, write, end, close), for any other protocol. |
session.info |
mode, transport, end_to_end_encrypted, trust, peer_address, direct_failure_reason. |
await session.close(), await client.close() |
End the session, or everything. |
Errors are ConnectivitizerError with a stable code (E2E_SHARED_KEY_MISMATCH, DEVICE_OFFLINE, FORBIDDEN, …; see Error codes). RATE_LIMITED is never final: error.retry_after says in how many seconds to ask again.
A session does not depend on the connection to the platform: when that connection is lost, open sessions keep running, the client reconnects by itself and renews their leases over the new connection. Only a revocation or the end of a lease ends them.
await link_app("Report service", on_prompt) links a program to an account instead of giving it a key: the person approves in the web app and the program receives an API key of its own (LINK_DENIED, LINK_EXPIRED when not).
Device side
from connectivitizer import Agent
agent = Agent(device_key="czd_…", name="nas", shared_key="czs_…",
services={"api": "127.0.0.1:8080"}, log=print)
await agent.run() # until await agent.stop(); reconnects by itself, raises when the key is revoked
The device appears in the workspace the first time it connects. Service(target, protocol, allow=["key:<name>"]) restricts a service to holders of one of the device's keys.
A device that calls another device
A running device opens sessions to other devices over its own connection to the platform. It needs no API key: the platform uses the rights of the device key the device logs in with.
- The device key has "Its devices may open sessions to other devices" switched on (web app: Credentials). Without it the platform refuses with
FORBIDDEN. - The device holds the other device's shared key. With several keys, the one named like the other device is used, else the only one, else the one named
default.
agent = Agent(device_key="czd_…", name="hall-panel", shared_key="czs_…",
services={"api": "127.0.0.1:8080"}, log=print)
running = asyncio.ensure_future(agent.run())
session = await agent.connect("living-room-lamp", ["api"]) # waits until the device is connected
response = await session.fetch("api", "/state")
print(response.status, await response.json(), session.info.mode)
await agent.connect(device, services) |
Opens a session to another device by its name. The session is the same object the calling side gets (fetch, websocket, open_stream, info, close). |
await agent.list_devices() |
The devices this device may reach, without itself: name, alias, online, and the services it may use. |
agent.outgoing_sessions |
The info of the sessions this device opened that are still running. agent.open_sessions lists the sessions opened to it. |
Sessions the device opened stay open while it reconnects to the platform, and end when the device stops, or when the device or its device key is revoked.
Direct sessions
Nothing to configure. Platform binds a dual-stack UDP socket on a port the system picks and finds the machine's addresses itself (the source address of each default route, the addresses of the host name, and on Linux the kernel's IPv6 list); the SDK then offers the local IPv4 address, global IPv6 addresses and the public address STUN reports, and punches through the routers. Platform(ssl_context=…) trusts the certificate authority of a self-hosted installation.
What is in the package
noise.py, mux.py, session.py |
The shared-key channel (Noise NNpsk0), yamux multiplexing, the session bind and service streams. |
signaling.py, relay_path.py, api.py, grants.py |
The control plane, the relay, grant verification (ES256). |
direct/ |
Addresses, STUN and hole punching, KCP (a port of the reference ikcp.c), the KCP path. |
http.py, websocket.py |
HTTP/1.1 and WebSocket over any byte stream: to the platform and to the device's services. |
platform.py |
asyncio sockets, UDP and local addresses. |
interop/ |
Programs for the interoperability tests with the other implementations. |
Checks
python -m venv .venv && .venv/Scripts/pip install -e . # .venv/bin on Linux and macOS
.venv/Scripts/python -m unittest discover -s tests -t . # every layer in memory
Each release is also tested against the control plane and the relay with the .NET library and the TypeScript SDK on the other side, in both directions, relayed and peer to peer.
Limits
- The shared key is the only end-to-end layer. Certificates with pairing, and enrollment with a one-time token, are in
cz, the .NET library and the TypeScript SDK on Node.js. - Direct sessions use UDP. Direct connections over TCP are in
czand the .NET library only. - A device run by this SDK cannot be reached in compatibility mode (through the gateway).
Metadata
Release files for huelake-connectivitizer 1.0.128
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| huelake_connectivitizer-1.0.128.tar.gz | 91.6 kB | Without provenance | Details for huelake_connectivitizer-1.0.128.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| huelake_connectivitizer-1.0.128-py3-none-any.whl | Python 3 | none | any | Details for huelake_connectivitizer-1.0.128-py3-none-any.whl |
Total release size: 177.1 kB
Release files / huelake_connectivitizer-1.0.128.tar.gz
| Download URL | huelake_connectivitizer-1.0.128.tar.gz |
|---|---|
| Size | 91.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
caf2ce0002c7e1b2e1ea018b73535a71e8fbc7acd0899a762b20f692e11e700c
|
|
BLAKE2b-256 checksum How to use checksums |
835893fcf02b9e105062eb862e5def8d2b989eeb9f19929bf1f9c27ff763e461
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / huelake_connectivitizer-1.0.128-py3-none-any.whl
| Download URL | huelake_connectivitizer-1.0.128-py3-none-any.whl |
|---|---|
| Size | 85.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d0cba99ef8e18595ae0b6b9ea235076af70752f6abe3a1d9b39fb65f0c74245f
|
|
BLAKE2b-256 checksum How to use checksums |
3f6e0c3a49940321bb8974775361486ef511e90f79a9999738e2e0d4ea627d37
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Security for huelake-connectivitizer 1.0.128
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.