Skip to main content

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 cz and 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.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for huelake-connectivitizer 1.1.5
File Size Uploaded Provenance Actions
huelake_connectivitizer-1.1.5.tar.gz 92.1 kB Without provenance Details for huelake_connectivitizer-1.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for huelake-connectivitizer 1.1.5
File Interpreter ABI Platform Actions
huelake_connectivitizer-1.1.5-py3-none-any.whl Python 3 none any Details for huelake_connectivitizer-1.1.5-py3-none-any.whl

Total release size: 177.8 kB

Release files / huelake_connectivitizer-1.1.5.tar.gz

Download URL huelake_connectivitizer-1.1.5.tar.gz
Size 92.1 kB
Tags Source
SHA-256 checksum
How to use checksums
25cb0889ad69b83eb453ead6e41a580feff7c9d437ebc09a8a4646cde363646c
BLAKE2b-256 checksum
How to use checksums
820d0d9123eb467f676172901f1112b21272f720c04d02b7496079d963c941c4
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.1.5-py3-none-any.whl

Download URL huelake_connectivitizer-1.1.5-py3-none-any.whl
Size 85.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
30647086c5695f0bb61b198afd69353b96be2fb684d007e898f5094fad81ef35
BLAKE2b-256 checksum
How to use checksums
fd4653a257f91ce83ed8481e33b7b2845d66f66521d3af1de893f1930cf9b934
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.1.5

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.

Release history Release notifications | RSS feed

This release

1.1.5 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page