ws4py Package¶
ws4py Package¶
exc Module¶
-
exception
ws4py.exc.ProtocolException[source]¶ Bases:
ws4py.exc.WebSocketException
-
exception
ws4py.exc.FrameTooLargeException[source]¶ Bases:
ws4py.exc.WebSocketException
-
exception
ws4py.exc.UnsupportedFrameTypeException[source]¶ Bases:
ws4py.exc.WebSocketException
-
exception
ws4py.exc.UnsupportedFrameTypeException[source] Bases:
ws4py.exc.WebSocketException
-
exception
ws4py.exc.TextFrameEncodingException[source]¶ Bases:
ws4py.exc.WebSocketException
-
exception
ws4py.exc.TextFrameEncodingException[source] Bases:
ws4py.exc.WebSocketException
-
exception
ws4py.exc.InvalidBytesError[source]¶ Bases:
ws4py.exc.WebSocketException
-
exception
ws4py.exc.HandshakeError(msg)[source]¶ Bases:
ws4py.exc.WebSocketException
framing Module¶
-
class
ws4py.framing.Frame(opcode=None, body='', masking_key=None, fin=0, rsv1=0, rsv2=0, rsv3=0)[source]¶ Bases:
objectImplements the framing protocol as defined by RFC 6455.
1 2 3 4 5 6 7 8 9 10
>>> test_mask = 'XXXXXX' # perhaps from os.urandom(4) >>> f = Frame(OPCODE_TEXT, 'hello world', masking_key=test_mask, fin=1) >>> bytes = f.build() >>> bytes.encode('hex') '818bbe04e66ad6618a06d1249105cc6882' >>> f = Frame() >>> f.parser.send(bytes[0]) 1 >>> f.parser.send(bytes[1]) 4
See also
Data Framing http://tools.ietf.org/html/rfc6455#section-5.2
-
parser¶
-
build()[source]¶ Builds a frame from the instance’s attributes and returns its bytes representation.
-
mask(data)[source]¶ Performs the masking or unmasking operation on data using the simple masking algorithm:
-
unmask(data)¶ Performs the masking or unmasking operation on data using the simple masking algorithm:
-
manager Module¶
The manager module provides a selected classes to handle websocket’s execution.
Initially the rationale was to:
- Externalize the way the CherryPy server had been setup as its websocket management was too tightly coupled with the plugin implementation.
- Offer a management that could be used by other server or client implementations.
- Move away from the threaded model to the event-based model by relying on select or epoll (when available).
A simple usage for handling websocket clients:
from ws4py.client import WebSocketBaseClient
from ws4py.manager import WebSocketManager
m = WebSocketManager()
class EchoClient(WebSocketBaseClient):
def handshake_ok(self):
m.add(self) # register the client once the handshake is done
def received_message(self, msg):
print str(msg)
m.start()
client = EchoClient('ws://localhost:9000/ws')
client.connect()
m.join() # blocks forever
Managers are not compulsory but hopefully will help your workflow. For clients, you can still rely on threaded, gevent or tornado based implementations of course.
-
class
ws4py.manager.SelectPoller(timeout=0.1)[source]¶ Bases:
objectA socket poller that uses the select implementation to determines which file descriptors have data available to read.
It is available on all platforms.
-
class
ws4py.manager.EPollPoller(timeout=0.1)[source]¶ Bases:
objectAn epoll poller that uses the
epollimplementation to determines which file descriptors have data available to read.Available on Unix flavors mostly.
-
class
ws4py.manager.KQueuePoller(timeout=0.1)[source]¶ Bases:
objectAn epoll poller that uses the
epollimplementation to determines which file descriptors have data available to read.Available on Unix flavors mostly.
-
class
ws4py.manager.WebSocketManager(poller=None)[source]¶ Bases:
threading.ThreadAn event-based websocket manager. By event-based, we mean that the websockets will be called when their sockets have data to be read from.
The manager itself runs in its own thread as not to be the blocking mainloop of your application.
The poller’s implementation is automatically chosen with
epollif available elseselectunless you provide your ownpoller.-
add(websocket)[source]¶ Manage a new websocket.
First calls its
opened()method and register its socket against the poller for reading events.
-
remove(websocket)[source]¶ Remove the given
websocketfrom the manager.This does not call its
closed()method as it’s out-of-band by your application or from within the manager’s run loop.
-
run()[source]¶ Manager’s mainloop executed from within a thread.
Constantly poll for read events and, when available, call related websockets’ once method to read and process the incoming data.
If the
once()method returns a False value, itsterminate()method is also applied to properly close the websocket and its socket is unregistered from the poller.Note that websocket shouldn’t take long to process their data or they will block the remaining websockets with data to be handled. As for what long means, it’s up to your requirements.
-
messaging Module¶
-
class
ws4py.messaging.Message(opcode, data='', encoding='utf-8')[source]¶ Bases:
objectA message is a application level entity. It’s usually built from one or many frames. The protocol defines several kind of messages which are grouped into two sets:
- data messages which can be text or binary typed
- control messages which provide a mechanism to perform in-band control communication between peers
The
opcodeindicates the message type anddatais the possible message payload.The payload is held internally as a a
bytearrayas they are faster than pure strings for append operations.Unicode data will be encoded using the provided
encoding.-
single(mask=False)[source]¶ Returns a frame bytes with the fin bit set and a random mask.
If
maskis set, automatically mask the frame using a generated 4-byte token.
-
fragment(first=False, last=False, mask=False)[source]¶ Returns a
ws4py.framing.Framebytes.The behavior depends on the given flags:
first: the frame usesself.opcodeelse a continuation opcodelast: the frame has itsfinbit setmask: the frame is masked using a automatically generated 4-byte token
-
completed¶ Indicates the the message is complete, meaning the frame’s
finbit was set.
-
class
ws4py.messaging.TextMessage(text=None)[source]¶ Bases:
ws4py.messaging.Message-
is_binary¶
-
is_text¶
-
-
class
ws4py.messaging.BinaryMessage(bytes=None)[source]¶ Bases:
ws4py.messaging.Message-
is_binary¶
-
is_text¶
-
-
class
ws4py.messaging.CloseControlMessage(code=1000, reason='')[source]¶ Bases:
ws4py.messaging.Message
-
class
ws4py.messaging.PingControlMessage(data=None)[source]¶ Bases:
ws4py.messaging.Message
-
class
ws4py.messaging.PongControlMessage(data)[source]¶ Bases:
ws4py.messaging.Message
streaming Module¶
-
class
ws4py.streaming.Stream(always_mask=False, expect_masking=True)[source]¶ Bases:
objectRepresents a websocket stream of bytes flowing in and out.
The stream doesn’t know about the data provider itself and doesn’t even know about sockets. Instead the stream simply yields for more bytes whenever it requires them. The stream owner is responsible to provide the stream with those bytes until a frame can be interpreted.
1 2 3 4 5 6 7 8 9
>>> s = Stream() >>> s.parser.send(BYTES) >>> s.has_messages False >>> s.parser.send(MORE_BYTES) >>> s.has_messages True >>> s.message <TextMessage ... >
Set
always_maskto mask all frames built.Set
expect_maskingto indicate masking will be checked on all parsed frames.-
message= None¶ Parsed test or binary messages. Whenever the parser reads more bytes from a fragment message, those bytes are appended to the most recent message.
-
pings= None¶ Parsed ping control messages. They are instances of
ws4py.messaging.PingControlMessage
-
pongs= None¶ Parsed pong control messages. They are instances of
ws4py.messaging.PongControlMessage
-
closing= None¶ Parsed close control messsage. Instance of
ws4py.messaging.CloseControlMessage
-
errors= None¶ Detected errors while parsing. Instances of
ws4py.messaging.CloseControlMessage
-
parser¶
-
text_message(text)[source]¶ Returns a
ws4py.messaging.TextMessageinstance ready to be built. Convenience method so that the caller doesn’t need to import thews4py.messaging.TextMessageclass itself.
-
binary_message(bytes)[source]¶ Returns a
ws4py.messaging.BinaryMessageinstance ready to be built. Convenience method so that the caller doesn’t need to import thews4py.messaging.BinaryMessageclass itself.
-
has_message¶ Checks if the stream has received any message which, if fragmented, is now completed.
-
close(code=1000, reason='')[source]¶ Returns a close control message built from a
ws4py.messaging.CloseControlMessageinstance, using the given statuscodeandreasonmessage.
-
ping(data='')[source]¶ Returns a ping control message built from a
ws4py.messaging.PingControlMessageinstance.
-
pong(data='')[source]¶ Returns a ping control message built from a
ws4py.messaging.PongControlMessageinstance.
-
receiver()[source]¶ Parser that keeps trying to interpret bytes it is fed with as incoming frames part of a message.
Control message are single frames only while data messages, like text and binary, may be fragmented accross frames.
The way it works is by instanciating a
wspy.framing.Frameobject, then running its parser generator which yields how much bytes it requires to performs its task. The stream parser yields this value to its caller and feeds the frame parser.When the frame parser raises
StopIteration, the stream parser tries to make sense of the parsed frame. It dispatches the frame’s bytes to the most appropriate message type based on the frame’s opcode.Overall this makes the stream parser totally agonstic to the data provider.
-
utf8validator Module¶
-
class
ws4py.utf8validator.Utf8Validator[source]¶ Bases:
objectIncremental UTF-8 validator with constant memory consumption (minimal state).
Implements the algorithm “Flexible and Economical UTF-8 Decoder” by Bjoern Hoehrmann (http://bjoern.hoehrmann.de/utf-8/decoder/dfa/).
-
UTF8VALIDATOR_DFA= [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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 1¶
-
decode(b)[source]¶ Eat one UTF-8 octet, and validate on the fly.
Returns UTF8_ACCEPT when enough octets have been consumed, in which case self.codepoint contains the decoded Unicode code point.
Returns UTF8_REJECT when invalid UTF-8 was encountered.
Returns some other positive integer when more octets need to be eaten.
-
validate(ba)[source]¶ Incrementally validate a chunk of bytes provided as bytearray.
Will return a quad (valid?, endsOnCodePoint?, currentIndex, totalIndex).
As soon as an octet is encountered which renders the octet sequence invalid, a quad with valid? == False is returned. currentIndex returns the index within the currently consumed chunk, and totalIndex the index within the total consumed sequence that was the point of bail out. When valid? == True, currentIndex will be len(ba) and totalIndex the total amount of consumed bytes.
-
websocket Module¶
-
class
ws4py.websocket.Heartbeat(websocket, frequency=2.0)[source]¶ Bases:
threading.ThreadRuns at a periodic interval specified by frequency by sending an unsolicitated pong message to the connected peer.
If the message fails to be sent and a socket error is raised, we close the websocket socket automatically, triggering the closed handler.
-
class
ws4py.websocket.WebSocket(sock, protocols=None, extensions=None, environ=None, heartbeat_freq=None)[source]¶ Bases:
objectThe
sockis an opened connection resulting from the websocket handshake.If
protocolsis provided, it is a list of protocols negotiated during the handshake as isextensions.If
environis provided, it is a copy of the WSGI environ dictionnary from the underlying WSGI server.-
stream= None¶ Underlying websocket stream that performs the websocket parsing to high level objects. By default this stream never masks its messages. Clients using this class should set the
stream.always_maskfields toTrueandstream.expect_maskingfields toFalse.
-
protocols= None¶ List of protocols supported by this endpoint. Unused for now.
-
extensions= None¶ List of extensions supported by this endpoint. Unused for now.
-
sock= None¶ Underlying connection.
-
client_terminated= None¶ Indicates if the client has been marked as terminated.
-
server_terminated= None¶ Indicates if the server has been marked as terminated.
-
reading_buffer_size= None¶ Current connection reading buffer size.
-
environ= None¶ WSGI environ dictionary.
-
heartbeat_freq= None¶ At which interval the heartbeat will be running. Set this to 0 or None to disable it entirely.
-
local_address¶ Local endpoint address as a tuple
-
peer_address¶ Peer endpoint address as a tuple
-
close(code=1000, reason='')[source]¶ Call this method to initiate the websocket connection closing by sending a close frame to the connected peer. The
codeis the status code representing the termination’s reason.Once this method is called, the
server_terminatedattribute is set. Calling this method several times is safe as the closing frame will be sent only the first time.See also
Defined Status Codes http://tools.ietf.org/html/rfc6455#section-7.4.1
-
closed(code, reason=None)[source]¶ Called when the websocket stream and connection are finally closed. The provided
codeis status set by the other point andreasonis a human readable message.See also
Defined Status Codes http://tools.ietf.org/html/rfc6455#section-7.4.1
-
terminated¶ Returns
Trueif both the client and server have been marked as terminated.
-
connection¶
-
ponged(pong)[source]¶ Pong message, as a
messaging.PongControlMessageinstance, received on the stream.
-
received_message(message)[source]¶ Called whenever a complete
message, binary or text, is received and ready for application’s processing.The passed message is an instance of
messaging.TextMessageormessaging.BinaryMessage.Note
You should override this method in your subclass.
-
send(payload, binary=False)[source]¶ Sends the given
payloadout.If
payloadis some bytes or a bytearray, then it is sent as a single message not fragmented.If
payloadis a generator, each chunk is sent as part of fragmented message.If
binaryis set, handles the payload as a binary message.
-
once()[source]¶ Performs the operation of reading from the underlying connection in order to feed the stream of bytes.
We start with a small size of two bytes to be read from the connection so that we can quickly parse an incoming frame header. Then the stream indicates whatever size must be read from the connection since it knows the frame payload length.
It returns False if an error occurred at the socket level or during the bytes processing. Otherwise, it returns True.
-
terminate()[source]¶ Completes the websocket by calling the closed method either using the received closing code and reason, or when none was received, using the special 1006 code.
Finally close the underlying connection for good and cleanup resources by unsetting the environ and stream attributes.
-
process(bytes)[source]¶ Takes some bytes and process them through the internal stream’s parser. If a message of any kind is found, performs one of these actions:
- A closing message will initiate the closing handshake
- Errors will initiate a closing handshake
- A message will be passed to the
received_messagemethod - Pings will see pongs be sent automatically
- Pongs will be passed to the
pongedmethod
The process should be terminated when this method returns
False.
-
run()[source]¶ Performs the operation of reading from the underlying connection in order to feed the stream of bytes.
We start with a small size of two bytes to be read from the connection so that we can quickly parse an incoming frame header. Then the stream indicates whatever size must be read from the connection since it knows the frame payload length.
Note that we perform some automatic opererations:
- On a closing message, we respond with a closing message and finally close the connection
- We respond to pings with pong messages.
- Whenever an error is raised by the stream parsing, we initiate the closing of the connection with the appropiate error code.
This method is blocking and should likely be run in a thread.
-
-
class
ws4py.websocket.EchoWebSocket(sock, protocols=None, extensions=None, environ=None, heartbeat_freq=None)[source]¶ Bases:
ws4py.websocket.WebSocketThe
sockis an opened connection resulting from the websocket handshake.If
protocolsis provided, it is a list of protocols negotiated during the handshake as isextensions.If
environis provided, it is a copy of the WSGI environ dictionnary from the underlying WSGI server.