Skip to main content

WebSockets in pure Python — RFC 6455 server and client with framing, masking, ping/pong, and fragmentation. No async, zero dependencies.

Project description

larzws

WebSockets in pure Python. Zero dependencies.

A real RFC 6455 WebSocket server and client with nothing to install — no websockets, no aiohttp, no async runtime. Handshake, framing, masking, fragmentation, ping/pong, and close are handled; you just send and receive.

from larzws import serve, connect

# server — echo
def handler(ws):
    for message in ws:                 # iterate messages until the client leaves
        ws.send("echo: " + message)
serve(handler, "127.0.0.1", 8765)

# client
ws = connect("ws://127.0.0.1:8765")
ws.send("hello")
print(ws.recv())                       # "echo: hello"
ws.close()

What makes it different

  • No async, no dependency. Most WebSocket libraries drag in an async stack. larzws is plain sockets + threads — drop it into any script or service.
  • Server and client, both real. Full RFC 6455: the Upgrade handshake, binary framing with 7/16/64-bit lengths, client-side masking, fragmentation reassembly, automatic ping→pong, and clean close.
  • Text and binary. str messages go as text frames, bytes as binary; you get back the right type.
  • Easy to embed. Threaded server (one thread per connection) with start_background() for tests and apps.

Pairs naturally with larzrpc for realtime request/response over a socket.

Install

pip install larzws

Server

from larzws import WebSocketServer, serve

def handler(ws):
    ws.send("welcome")
    for message in ws:
        ws.send("you said: " + message)

serve(handler, "0.0.0.0", 8765)          # blocking

# or embed it
server = WebSocketServer("127.0.0.1", 8765, handler)
server.start_background()                # daemon thread; server.port is resolved
...
server.shutdown()

Client

from larzws import connect

ws = connect("ws://127.0.0.1:8765/chat")
ws.send("hi")               # text
ws.send(b"\x00\x01")        # binary
msg = ws.recv()             # str or bytes, or None when closed
for msg in ws:              # or iterate until close
    ...
ws.ping()
ws.close()

Scope

larzws speaks plain ws:// (not wss:///TLS in this version — terminate TLS at a reverse proxy if you need it) and uses a thread per connection, which is ideal for tools, internal services, dashboards, and realtime features up to moderate concurrency. For tens of thousands of idle connections you'd want an async library; for everything else, this is refreshingly simple.

Tests

python -m unittest discover -s tests -v   # 14 tests: RFC handshake, framing, e2e echo

The Larz stack

Pure-Python, zero-dependency building blocks: larz · larzchain · larzmoney · larzcrypt · larzdb · larzagent · larzchart · larzmark · larztask · larzvault · larzvm · larzcache · larzvalidate · larzid · larzrpc · larzstate · larzhttp · larzconf · larzcron · larzlimit · larzlog · larzcli · larzretry · larztime · larzpdf · larzpack · larztemplate · larzcolor · larztable · larzjson · larzbus · larzmigrate · larzgraph · larzws

License

MIT © larz-scripter

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

larzws-0.1.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

larzws-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file larzws-0.1.0.tar.gz.

File metadata

  • Download URL: larzws-0.1.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzws-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7d702b4e00413fea64a83d1411b919a4657da9c79b7c6ad0e422045267a8bf5b
MD5 018471194cd00150fdb3dac57cf8f3ad
BLAKE2b-256 df1a5bd3d73a1caa65a3c7c26aa6bb02f1723eeaee4728b8f6b4810ab4e511ca

See more details on using hashes here.

File details

Details for the file larzws-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: larzws-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzws-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2faca701e6ea3b046f00460909e475622fbb4ba64032f42447b3441fcc7df58
MD5 6128f672db4921f039dc7bb7c828ede2
BLAKE2b-256 fde07ff1c921b073aa8a19b34cd5dc7c9674ad128fe4eac555633d9f7c4f7e26

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page