Registry / http-networking / websockets

websockets

JSON →
library17.1pypypi✓ verified 26d ago

An implementation of the WebSocket Protocol (RFC 6455 & 7692) for Python, currently at version 16.0, with a release cadence of approximately every 6 months.

pip install websockets
INSTALL
IMPORT
SIG · WEBSOCKETS
W
websockets
http-networkingpythonv17.1
Install
2.0s avg
Import
300ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v16.1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.316s · 19.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.0s · import 0.284s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

serve
from websockets import serve
Ensure correct import path to avoid 'NameError'
connect
from websockets import connect
Ensure correct import path to avoid 'NameError'

A simple WebSocket server that echoes messages received from clients.

import asyncio from websockets import serve async def handler(websocket, path): async for message in websocket: await websocket.send(f'Hello, {message}!') async def main(): async with serve(handler, 'localhost', 8765): await asyncio.Future() # Run forever if __name__ == '__main__': asyncio.run(main())
Debug
Known issues
breakingwebsockets 16.0 requires Python ≥ 3.10; 15.0 is the last version supporting Python 3.9.
fix
Upgrade to Python 3.10 or later.
affects: 16.0
breakingReceiving the request path in the second parameter of connection handlers is deprecated; use 'path = request.path' inside the handler function.
fix
Update handler functions to access 'path' via 'request.path'.
affects: 13.0
gotchaEnsure correct import paths to avoid 'NameError' when importing 'serve' or 'connect'.
fix
Use 'from websockets import serve' and 'from websockets import connect'.
affects: All
breakingThe application timed out. This may indicate the websockets server failed to start, became unresponsive due to an internal error or deadlock, or could not be reached by the client.
fix
Examine server logs for startup errors, unhandled exceptions, or deadlocks. Verify network connectivity and port availability. Ensure `websockets.serve` is properly awaited and running without blocking issues.
affects: All
Errors
Common errors & fixes
ConnectionRefusedError: [Errno 111] Connection refused
The WebSocket client failed to connect because the server was not running, was not listening, or was not accessible at the specified address and port.
fix
Ensure the WebSocket server is active and reachable at the correct host and port before the client attempts to connect.
RuntimeError: Event loop is closed
An `asyncio` operation, such as creating a new task or awaiting a coroutine, was attempted on an event loop that had already been shut down.
fix
Ensure that `asyncio.run()` is called only once to manage the event loop, or explicitly manage the loop lifecycle to avoid using a closed loop.
websockets.exceptions.WebSocketProtocolError: WebSocket connection upgrade failed (400 Bad Request)
The WebSocket handshake failed because the server rejected the client's request due to a protocol violation, invalid headers, or an issue on the server side.
fix
Review the client's handshake request (e.g., headers, URL) and server logs to identify and resolve the specific protocol discrepancy.
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
The client could not establish a secure TLS/SSL connection because the server's certificate was invalid, expired, untrusted, or did not match the hostname.
fix
Verify the server's SSL certificate is valid and issued by a trusted CA, or, for testing, consider passing `ssl=False` or `ssl_context.check_hostname = False` (use with caution).
TypeError: a bytes-like object is required, not 'str'
An attempt was made to use a `str` object where `bytes` were expected (e.g., when sending data) or vice versa (e.g., when decoding received data) without proper encoding/decoding.
fix
Encode strings to bytes before sending (`my_string.encode('utf-8')`) and decode received bytes to strings (`my_bytes.decode('utf-8')`) as appropriate.
Upgrade
Version history
17.1latest on PyPI · released Aug 26, 2026
Audit
Dependencies
asynciorequiredRequired for asynchronous operations in Python 3.10+
Agent activity
25 hits · last 30 days
node
22
OpenAI (training)
1
Resources