Skip to main content

clavem

Small async TLS-PSK message channels. Python 3.13+, no third-party runtime dependencies.

Clavem authenticates peers with a shared random key and carries length-prefixed byte messages over one TLS 1.3 connection. It does not reconnect, schedule heartbeats, serialize application data, or configure logging.

Install

pip install clavem

For an unpublished checkout, use uv sync and uv run instead. Both endpoints need a Python/OpenSSL build with PSK support:

python -c "import ssl; print(ssl.OPENSSL_VERSION, ssl.HAS_PSK)"

HAS_PSK must be True. Check this on the exact interpreter used for deployment and on the interpreter bundled into a desktop executable.

Connect and exchange messages

Create one 32-byte random key and securely provision the same bytes on both peers. Use a different key for unrelated services. The identity is public, not a password.

from pathlib import Path
import asyncio

from clavem.transport import connect


async def main() -> None:
    key = Path("psk.bin").read_bytes()
    async with await connect("127.0.0.1", 9443, identity="home", key=key) as channel:
        await channel.send(b"hello")
        async with asyncio.timeout(5):
            reply = await channel.receive()
        print(reply)


asyncio.run(main())

One task receives messages; multiple tasks may send, with complete frames serialized by the channel. TCP_NODELAY is enabled on both peers: the client uses asyncio's default, and the server explicitly enables it on accepted TLS sockets. Messages can be empty and are limited to 65,536 bytes. The wire format is a four-byte unsigned network-order length followed by that many bytes.

Serve

from pathlib import Path
import asyncio

from clavem.channel import Channel
from clavem.transport import serve


async def echo(channel: Channel) -> None:
    try:
        while True:
            async with asyncio.timeout(30):
                message = await channel.receive()
                await channel.send(message)
    except (asyncio.IncompleteReadError, ConnectionError, TimeoutError):
        return


async def main() -> None:
    key = Path("psk.bin").read_bytes()
    async with serve(echo, "127.0.0.1", 9443, keys={"home": key}) as server:
        await server.serve_forever()


asyncio.run(main())

serve() owns accepted channels and handler tasks. Returning from a handler closes its channel. Leaving the server context closes the listener and cancels and awaits remaining handlers. Handle expected peer disconnects in your handler; unexpected handler exceptions stop the server and propagate through its task group.

Contract

API Behavior
connect(..., timeout=5) Bounds TCP connection and TLS authentication together. Returns a Channel.
serve(..., handshake_timeout=5) Async context manager yielding an asyncio.Server.
Channel.send(bytes) Checks the size before writing and applies stream backpressure.
Channel.receive() Reads a complete frame; EOF raises asyncio.IncompleteReadError.
Channel.close() Closes the stream; supported by async with channel.
client_context() / server_context() TLS contexts for applications that need lower-level asyncio integration.

An oversized incoming frame raises clavem.channel.MessageTooLarge and closes the stream. An oversized outgoing message raises the same error before writing. Network, TLS, and timeout exceptions retain their standard-library types. After cancelling or timing out an in-progress receive or send, close the channel rather than trying to reuse a potentially partial exchange.

TLS contexts require external PSK authentication. The client loads no trusted CAs, the server loads no certificate and issues no session tickets, and the transport checks that PSK authentication was negotiated. There is no plaintext fallback. Clavem sets neither application idle timeouts nor connection-count limits; the hosting application owns those policies.

Development and release

Use uv sync for the environment and committed uv.lock. Run uv run pytest for real loopback TLS regressions: normal PSK exchange, rejection of untrusted and trusted certificate fallback, and TCP_NODELAY checks on both peers. The publish workflow runs these checks before building distributions.

Versions live in pyproject.toml and uv.lock. Release with a matching vX.Y.Z tag after configuring the repository's pypi environment and the PyPI Trusted Publisher. The workflow builds distributions, verifies an isolated wheel import, publishes to PyPI, and attaches distributions to a GitHub Release.

Metadata

Release files for clavem 0.1.0

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

Source distribution (sdist)

Source distribution for clavem 0.1.0
File Size Uploaded Provenance Actions
clavem-0.1.0.tar.gz 14.9 kB With provenance Details for clavem-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for clavem 0.1.0
File Interpreter ABI Platform Actions
clavem-0.1.0-py3-none-any.whl Python 3 none any Details for clavem-0.1.0-py3-none-any.whl

Total release size: 22.2 kB

Release files / clavem-0.1.0.tar.gz

Download URL clavem-0.1.0.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6012a8d69903ccd0dbac047ca039d8057bd5ec4cc99b225799b879a5a533dbba
BLAKE2b-256 checksum
How to use checksums
490fbee84855e1d671d27eef63d4de2cc7a6f25af89cf75a909af19fdc148a8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

Release files / clavem-0.1.0-py3-none-any.whl

Download URL clavem-0.1.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a16b9e324b00be2aba672021266738e15a9c16428cf88397d026eda226ce3618
BLAKE2b-256 checksum
How to use checksums
1b5223141b45506bbb80e52b693c5e636428d0eab3c96627f46505def4e90bdb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

Security for clavem 0.1.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for clavem-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

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

0.1.0 This release

2 release files Full 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