guv.websocket - websocket server

class guv.websocket.WebSocketWSGI(handler)[source]

Bases: object

Wraps a websocket handler function in a WSGI application.

Use it like this:

@websocket.WebSocketWSGI
def my_handler(ws):
    from_browser = ws.wait()
    ws.send("from server")

The single argument to the function will be an instance of WebSocket. To close the socket, simply return from the function. Note that the server will log the websocket request at the time of closure.

class guv.websocket.WebSocket(sock, environ, version=76)[source]

Bases: object

A websocket object that handles the details of serialization/deserialization to the socket.

The primary way to interact with a WebSocket object is to call send() and wait() in order to pass messages back and forth with the browser. Also available are the following properties:

path
The path value of the request. This is the same as the WSGI PATH_INFO variable, but more convenient.
protocol
The value of the Websocket-Protocol header.
origin
The value of the ‘Origin’ header.
environ
The full WSGI environment for this request.
__init__(sock, environ, version=76)[source]
Parameters:
  • socket (eventlet.greenio.GreenSocket) – The guv socket
  • environ – The wsgi environment
  • version – The WebSocket spec version to follow (default is 76)
close() → None[source]

Forcibly close the websocket; generally it is preferable to return from the handler method.

send(message) → None[source]

Send a message to the browser.

message should be convertable to a string; unicode objects should be encodable as utf-8. Raises socket.error with errno of 32 (broken pipe) if the socket has already been closed by the client.

wait() → None[source]

Waits for and deserializes messages.

Returns a single message; the oldest not yet processed. If the client has already closed the connection, returns None. This is different from normal socket behavior because the empty string is a valid websocket message.