{"id":7870,"library":"ws4py","title":"WebSocket-for-Python (ws4py)","description":"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.","status":"maintenance","version":"0.6.0","language":"en","source_language":"en","source_url":"https://github.com/Lawouach/WebSocket-for-Python","tags":["websocket","client","server","networking","threaded","gevent","eventlet"],"install":[{"cmd":"pip install ws4py","lang":"bash","label":"Install ws4py"}],"dependencies":[],"imports":[{"note":"WebSocketClient is located in a submodule; direct import from 'ws4py' will fail.","wrong":"from ws4py import WebSocketClient","symbol":"WebSocketClient","correct":"from ws4py.client.threadedclient import WebSocketClient"},{"note":"The base WebSocket handler class is directly under ws4py.websocket.","wrong":"from ws4py.server import WebSocket","symbol":"WebSocket","correct":"from ws4py.websocket import WebSocket"},{"note":"For a simple WSGI-compatible server using wsgiref. Other servers (gevent, eventlet) have their own specific imports and require additional dependencies.","symbol":"WebSocketServer","correct":"from ws4py.server.wsgirefserver import WebSocketWSGIRequestHandler, WSGIServer"}],"quickstart":{"code":"import time\nfrom ws4py.client.threadedclient import WebSocketClient\n\nclass DummyClient(WebSocketClient):\n    def opened(self):\n        print(\"WebSocket opened. Sending messages...\")\n        for i in range(0, 5):\n            self.send('Hello %d' % i)\n            time.sleep(0.5)\n        self.send('Bye')\n\n    def closed(self, code, reason=None):\n        print(\"WebSocket closed: %d %s\" % (code, reason))\n\n    def received_message(self, m):\n        print(\"Received: %s\" % m)\n        if m.data == b'Bye':\n            self.close()\n\n# Note: This client expects a WebSocket server to be running at ws://localhost:9000/\n# For a full runnable example, you'd also need a server like:\n# from ws4py.server.wsgirefserver import WSGIServer, WebSocketWSGIRequestHandler\n# from ws4py.websocket import WebSocket\n# from threading import Thread\n# class EchoWebSocket(WebSocket):\n#    def received_message(self, message):\n#        self.send(message.data, message.is_binary)\n# server = WSGIServer(('localhost', 9000), WebSocketWSGIRequestHandler, websocket_class=EchoWebSocket)\n# Thread(target=server.serve_forever).start()\n\nif __name__ == '__main__':\n    try:\n        ws = DummyClient('ws://localhost:9000/', protocols=['http-only', 'chat'])\n        ws.connect()\n        ws.run_forever()\n    except KeyboardInterrupt:\n        ws.close()","lang":"python","description":"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."},"warnings":[{"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`).","message":"Version 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.","severity":"breaking","affected_versions":"0.6.0+"},{"fix":"Install the required dependency: `pip install gevent` for `geventserver` or `pip install eventlet` for `eventletserver`.","message":"Using `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.","severity":"gotcha","affected_versions":"All"},{"fix":"For new projects or high-performance/asyncio requirements, consider more actively maintained alternatives like `websockets` (for asyncio) or `autobahn`.","message":"The 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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install the `gevent` package: `pip install gevent`.","cause":"Attempting to use `ws4py.server.geventserver.WebSocketServer` or `geventserver.WSGIWebSocketServer` without `gevent` installed.","error":"ImportError: No module named 'gevent'"},{"fix":"Ensure your `WebSocket` subclass is instantiated by the `WebSocketWSGIRequestHandler` or similar server component, not directly. If you're building a client, use `WebSocketClient`.","cause":"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`).","error":"TypeError: __init__() missing 1 required positional argument: 'sock'"},{"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.","cause":"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.","error":"WebSocket connection to 'ws://localhost:8000/' failed: Error during WebSocket handshake: Unexpected response code: 404"}]}