Websocket (gevent)

0.2.1 · abandoned · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic WebSocket client connection using the `websocket` library, patching `gevent` for its asynchronous operations. It attempts to connect to a public echo server and send/receive a message. Due to the library's age, compatibility with modern WebSocket servers is not guaranteed. Ensure `gevent` is installed alongside `websocket` for this to run.

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.")

view raw JSON →