Registry / http-networking / ws4py
library0.6.0pypypi✓ verified 86d ago

ws4py is a WebSocket client and server library for Python, supporting Python 3.6+ since version 0.6.0. It provides a straightforward API for building WebSocket applications, offering both threaded and eventlet/gevent-based server implementations. The library is currently in maintenance mode, with its last release (0.6.0) in 2021.

pip install ws4py
INSTALL
IMPORT
SIG · WS4PY
W
ws4py
http-networkingpythonv0.6.0
Install
1.5s avg
Import
63ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.6.0 · 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.920 runs
installs and imports cleanly · install 0.0s · import 0.065s · 18.1MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.5s · import 0.061s · 19MB
16MB installed
● package 16MB
Code
Verified usage

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

WebSocketClient
from ws4py.client.threadedclient import WebSocketClient
from ws4py import WebSocketClient
WebSocketClient is located in a submodule; direct import from 'ws4py' will fail.
WebSocket
from ws4py.websocket import WebSocket
from ws4py.server import WebSocket
The base WebSocket handler class is directly under ws4py.websocket.
WebSocketServer
from ws4py.server.wsgirefserver import WebSocketWSGIRequestHandler, WSGIServer
For a simple WSGI-compatible server using wsgiref. Other servers (gevent, eventlet) have their own specific imports and require additional dependencies.

A basic WebSocket client example using ws4py's threaded client. It connects to a server, sends a few messages, and closes. Note that a server must be running at `ws://localhost:9000/` for this client to successfully connect.

import time from ws4py.client.threadedclient import WebSocketClient class DummyClient(WebSocketClient): def opened(self): print("WebSocket opened. Sending messages...") for i in range(0, 5): self.send('Hello %d' % i) time.sleep(0.5) self.send('Bye') def closed(self, code, reason=None): print("WebSocket closed: %d %s" % (code, reason)) def received_message(self, m): print("Received: %s" % m) if m.data == b'Bye': self.close() # Note: This client expects a WebSocket server to be running at ws://localhost:9000/ # For a full runnable example, you'd also need a server like: # from ws4py.server.wsgirefserver import WSGIServer, WebSocketWSGIRequestHandler # from ws4py.websocket import WebSocket # from threading import Thread # class EchoWebSocket(WebSocket): # def received_message(self, message): # self.send(message.data, message.is_binary) # server = WSGIServer(('localhost', 9000), WebSocketWSGIRequestHandler, websocket_class=EchoWebSocket) # Thread(target=server.serve_forever).start() if __name__ == '__main__': try: ws = DummyClient('ws://localhost:9000/', protocols=['http-only', 'chat']) ws.connect() ws.run_forever() except KeyboardInterrupt: ws.close()
Debug
Known issues
breakingVersion 0.6.0 dropped official support for Python versions older than 3.6. Code relying on Python 2.x or 3.0-3.5 will break.
fix
Upgrade your Python environment to 3.6 or newer. If you must use older Python, pin ws4py to <0.6.0 (e.g., `pip install ws4py==0.5.1`).
affects: 0.6.0+
gotchaUsing `ws4py.server.geventserver` or `ws4py.server.eventletserver` requires the respective `gevent` or `eventlet` library to be installed separately. An `ImportError` will occur if they are missing.
fix
Install the required dependency: `pip install gevent` for `geventserver` or `pip install eventlet` for `eventletserver`.
affects: All
gotchaThe library's development is slow, with the last release in 2021. It might not include the latest WebSocket protocol extensions or offer modern asyncio-based concurrency patterns.
fix
For new projects or high-performance/asyncio requirements, consider more actively maintained alternatives like `websockets` (for asyncio) or `autobahn`.
affects: All
Errors
Common errors & fixes
ImportError: No module named 'gevent'
Attempting to use `ws4py.server.geventserver.WebSocketServer` or `geventserver.WSGIWebSocketServer` without `gevent` installed.
fix
Install the `gevent` package: `pip install gevent`.
TypeError: __init__() missing 1 required positional argument: 'sock'
This error often occurs when you try to instantiate a `WebSocket` subclass directly without passing the socket object, which is usually handled by the server wrapper (e.g., `WebSocketWSGIRequestHandler`).
fix
Ensure your `WebSocket` subclass is instantiated by the `WebSocketWSGIRequestHandler` or similar server component, not directly. If you're building a client, use `WebSocketClient`.
WebSocket connection to 'ws://localhost:8000/' failed: Error during WebSocket handshake: Unexpected response code: 404
The client attempted to connect to a WebSocket URL that either doesn't exist, is incorrect, or where no WebSocket server is running or properly configured to handle WebSocket requests.
fix
Verify the WebSocket server is running and listening on the correct host and port, and that the URL path (`/` in this example) is correctly mapped to a WebSocket handler on the server side.
Upgrade
Version history
0.6.0latest on PyPI · released Dec 19, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
28 hits · last 30 days
node
26
OpenAI (training)
1
Resources
ws4py — pip install ws4py · libregistry