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
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.065s · 18.1MB
glibcpy 3.10–3.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()
Errors
Common errors & fixes
ImportError: No module named 'gevent'
Attempting to use `ws4py.server.geventserver.WebSocketServer` or `geventserver.WSGIWebSocketServer` without `gevent` installed.
fixInstall 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`).
fixEnsure 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.
fixVerify 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.