Websocket (gevent)
The `websocket` library, version 0.2.1, released in 2011, provides a WebSocket client implementation specifically for use with the `gevent` asynchronous framework. This project is no longer actively maintained and is considered abandoned, making it unsuitable for modern Python development. Developers should use `websocket-client` for synchronous applications or `websockets` for asyncio-based applications instead.
Common errors
-
ImportError: cannot import name 'WebSocketApp' from 'websocket'
cause Attempting to use features from `websocket-client` (like `WebSocketApp`) after installing the old `websocket` package.fixYou have installed the wrong package. Uninstall `websocket` and install the correct, actively maintained library: `pip uninstall websocket && pip install websocket-client`. -
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
cause This generic socket error, when encountered with `websocket` 0.2.1, frequently indicates that the target WebSocket server actively rejected the connection due to an outdated protocol handshake or unsupported features, or a missing `gevent.monkey.patch_all()` call if gevent isn't properly initialized.fixEnsure `gevent.monkey.patch_all()` is called early in your script if you absolutely must use this library. More importantly, migrate to a modern WebSocket client library like `websocket-client` or `websockets` to ensure compatibility with contemporary servers.
Warnings
- breaking This library is fundamentally incompatible with modern WebSocket protocol versions and Python standards. Its last release was in 2011, predating significant advancements and RFC finalizations (e.g., RFC 6455). Using it will likely result in connection failures or unexpected behavior with contemporary WebSocket servers.
- gotcha The `websocket` package (0.2.1, gevent-based) is a distinct, abandoned library, often confused with the actively maintained `websocket-client` package. Importing `websocket` will install the old version, not the modern client.
- deprecated The library relies on a very old version of the WebSocket protocol (likely HyBi-00 to HyBi-07, pre-RFC 6455), which is not fully supported or implemented by most modern WebSocket servers. Connections will often fail during the handshake or exhibit unexpected frame handling.
Install
-
pip install websocket -
pip install websocket-client -
pip install websockets
Imports
- create_connection
from websocket import create_connection
- WebSocket
from websocket_client import WebSocket
from websocket import WebSocket
Quickstart
import os
import gevent
from gevent import monkey; monkey.patch_all()
from websocket import create_connection
try:
# Note: Many modern servers may not support the old protocol handshake
# This example connects to a public echo server that might be lenient.
ws_url = os.environ.get('WEBSOCKET_URL', 'ws://echo.websocket.events/')
print(f"Connecting to {ws_url} with gevent-based websocket...")
ws = create_connection(ws_url)
print("Sending 'Hello, World!'...")
ws.send("Hello, World!")
print("Receiving...")
result = ws.recv()
print(f"Received: '{result}'")
ws.close()
print("Connection closed.")
except Exception as e:
print(f"An error occurred: {e}")
print("This library is very old and may not be compatible with modern WebSocket servers or Python environments.")
print("Consider using 'websocket-client' or 'websockets' instead.")