Skip to main content

chumicro-sockets

One TCP / TLS / UDP socket surface across CircuitPython, MicroPython, and CPython.

One entry per socket shape (connector, listener, udp_socket) hides the per-runtime adapter selection; TLS is a tls= flag on each. Custom-CA TLS and server-side certificates work the same way on every runtime.


Part of the ChuMicro family: small, focused Python libraries for microcontrollers and laptops. Browse all libraries.

Install

# CircuitPython (after `circup bundle-add ChuMicro/ChuMicro-Bundle-Experimental`)
circup install chumicro_sockets

# MicroPython
mpremote mip install github:ChuMicro/ChuMicro-Bundle-Experimental/chumicro_sockets

# CPython
pip install chumicro-sockets-experimental

For bundle setup, pre-compiled .mpy bundles, the experimental channel, and details on PyPI naming, see the chumicro INSTALL guide.

Quick example

from chumicro_sockets import connector

# One connect state machine per runtime.  Runner-shaped apps register
# the connector with the runner (it exposes check / handle / io_*);
# one-shot scripts drive it to terminal inline.  On CircuitPython pass
# the board radio (with chumicro-wifi, radio=wifi.adapter.radio); the
# kwarg is ignored on MicroPython / CPython.
dial = connector("broker.example.com", 1883, radio=None)
while dial.state not in ("ready", "failed"):
    dial.tick(0)
if dial.state == "failed":
    raise dial.last_error
sock = dial.socket
sock.send(b"PING\r\n")
buffer = bytearray(128)
nbytes = sock.recv_into(buffer, 128)
print(bytes(buffer[:nbytes]))
sock.close()

# TLS is a flag: `tls=True` verifies the cert chain on every runtime.
# Each runtime gets its trust roots from the right place:
# CircuitPython's firmware bundle, CPython's OS trust store,
# MicroPython's library-shipped bundle (override via
# `set_default_ca_bundle`).  Pass `context=ssl_context_no_verify()`
# for explicit opt-out.
dial = connector("api.example.com", 443, tls=True)

CircuitPython always needs an explicit radio=. The socketpool is built from it (socketpool.SocketPool(radio)). Pass wifi.adapter.radio from a chumicro-wifi WifiService, or whatever radio object your board exposes. MicroPython and CPython ignore the kwarg.

For tests, chumicro_sockets.testing.FakeSocket implements the same protocol against in-memory bytearrays, so downstream libraries (chumicro-mqtt, chumicro-requests) can exercise their protocol handling without hitting the network.

What's included

Symbol Purpose
connector(host, port, *, tls=False, context=None, radio=None) Non-blocking tick-driven TCP/TLS connect, the one connect state machine. Register it with a runner or drive tick() to terminal inline.
listener(host, port, *, tls=False, context=None, backlog=4, radio=None) Open a non-blocking TCP or TLS listening socket.
udp_socket(bind_host="0.0.0.0", bind_port=0, *, radio=None, broadcast=False) Open a UDP datagram socket; default args bind ephemeral.
ssl_context_with_ca(ca_pem) Build an ssl.SSLContext trusting only the supplied CA(s). Works on every supported runtime.
ssl_context_no_verify() Build an ssl.SSLContext that skips certificate verification. Explicit opt-out, named so a reviewer can grep for it.
set_default_ca_bundle(pem_bytes) Replace the CA bundle used by connector(tls=True, context=None) on MicroPython. No-op on CP / CPython. Pass None to revert to the library-shipped bundle.
ssl_context_with_cert_and_key_paths(cert_path, key_path) Server-side ssl.SSLContext from PEM file paths. CP-portable shape.
TCP socket surface (duck-typed) send, recv_into, close, setblocking, settimeout. Any object exposing these works; no named Protocol class is exported.
UDP socket surface (duck-typed) sendto(data, host, port), recvfrom_into(buffer, nbytes=0) -> (n, (host, port)), close, setblocking. Any object exposing these works.
UnsupportedSSLConfigError Raised when the requested TLS shape isn't supported by the current runtime (e.g. CP's in-memory cert+key).
chumicro_sockets.testing.FakeSocket / FakeUDPSocket In-memory test doubles covering the full TCP / UDP protocol.

Where this fits

No runtime dependencies. On CircuitPython the caller passes a radio (wifi.adapter.radio from a chumicro-wifi WifiService, or any radio object the board exposes) from which the socketpool is built. Substrate for every networked library that follows: chumicro-requests, chumicro-http-server, chumicro-mqtt, chumicro-websockets, and chumicro-ntp.

Platform support

Works on CPython, MicroPython, and CircuitPython.

Examples

Example What it shows
tcp_roundtrip.py Real TCP connect → send → recv → close. Same shape on every runtime; pass the board radio via radio= on CircuitPython.
tls_with_custom_ca.py Custom-CA TLS via ssl_context_with_ca. Documents the substrate quirks observed on Pi Pico W mbedTLS in the docstring.
udp_echo_client.py Board-side UDP echo client: wifi up, send datagram to a host echo server, read echo back, non-blocking. Cross-runtime (CP + MP).

Contributing

Issues, bug reports, and pull requests are welcome, and so is "I ran it on this board and here's what happened", some of the most useful feedback a hardware project can get. Development happens in the ChuMicro repository, whose contributing guide covers setup and the test workflow.

Docs

📖 Stable docs · Experimental docs

Find this library

License

MIT

Metadata

Release files for chumicro-sockets-experimental 0.21.2

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

Source distribution (sdist)

Source distribution for chumicro-sockets-experimental 0.21.2
File Size Uploaded
chumicro_sockets_experimental-0.21.2.tar.gz 86.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chumicro-sockets-experimental 0.21.2
File Interpreter ABI Platform
chumicro_sockets_experimental-0.21.2-py3-none-any.whl Python 3 none any Details

Total release size: 123.5 kB

Release files / chumicro_sockets_experimental-0.21.2.tar.gz

Download URL chumicro_sockets_experimental-0.21.2.tar.gz
Size 86.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4f5e29740ff251e98c285e990f9dafe3e28c1d2e7519e37a42676e2faecd69ef
BLAKE2b-256 checksum
How to use checksums
c4f2041e01528e5bcdd9fca6a640a906ed542d40a82763487ca7820d8732a6fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Aug 18, 2026.

Transparency log

Release files / chumicro_sockets_experimental-0.21.2-py3-none-any.whl

Download URL chumicro_sockets_experimental-0.21.2-py3-none-any.whl
Size 37.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c920d5d87a372db06e16b6e5c394f9d62ed509c736f8863da10b5dcd78145e73
BLAKE2b-256 checksum
How to use checksums
c002fcf06171c274d8b3e4de8f5fb7d718e8ab356f57ce1a69c5cd0dd5e760e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Aug 18, 2026.

Transparency log

Release history Release notifications | RSS feed

0.21.3

2 release files

This release

0.21.2 This release

2 release files

0.20.0

2 release files

0.19.1

2 release files

0.19.0

2 release files

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