socktools.wsgi_websocket_sock

This module contains a simple wrapper around the websocket server support in eventlet, giving the same uniform API as the rest of socktools.

Once started, messages are read one by one from the socket, it is advised to use JSON for encoding and decoding.

class socktools.wsgi_websocket_sock.WSGIWebsocketSock(bind=None, connect=None, handlers={}, timeout=10, tick_interval=0.25, sock=None, meta_sock=None, logger=None, no_async=False)[source]

Bases: socktools.base_sock.BaseSock

Simple implementation of a WSGI websocket server.

This class implements a websocket server that can be used with any WSGI-compatible httpd. Call start_server() to startup the basic infrastructure and then get_wsgi_func() to get a WSGI function that can be used in your httpd.

create_socket()[source]

Does nothing

There is no concept of creating a physical socket for websockets, so this method does nothing.

get_wsgi_func()[source]

Get a WSGI function

Use this instead of directly referencing wsgi_func, directly referencing it will break subclasses that wrap the socket

Returns:the WSGI function that can be passed to an appropriate server
Return type:function
read_raw()[source]

Reads the next available raw packet from any client

In this implementation, that simply means grabbing from recv_q and returning

Returns:(data, from_addr) - data is a string, from_addr is the remote peer’s TCP/IP endpoint
Return type:tuple
send_raw(data, to_peer=None)[source]

Send a single raw packet to a specified peer (or to all connected peers)

This method essentially just dumps stuff into the specified peer’s sendq, or alternatively into every connected peer’s sendq.

It is then up to the handle_client_send thread to actually transmit the data

Parameters:data (str) – The raw data to send - must be already encoded including the length prefix
Keyword Arguments:
 to_peer (tuple) – The TCP/IP endpoint as a (host,ip) tuple, if set to None all connected peers will get the packet
start_server()[source]

Start the basic infrastructure so new clients can be accepted.

This method sets up the infrastructure needed to pass clients off to greenlets, it is then up to the user application to use get_wsgi_func() to do stuff with those clients.

Warning

Calling this twice is beyond idiotic, and not checked for - if you do that, it’s on you

ws_sender_thread(ws)[source]

Used internally - sends messages to websocket clients

Parameters:ws (websocket.WebSocket) – the websocket object representing the client connection
wsgi_func(ws)[source]

WSGI handler function

This is the WSGI handler function for websocket clients, it should not be invoked directly - use get_wsgi_func() and then pass that value to your httpd.

Parameters:ws (websocket.WebSocket) – the websocket object representing the client connection