Bases: ws4py.exc.WebSocketException
Bases: ws4py.exc.WebSocketException
Bases: ws4py.exc.WebSocketException
Bases: ws4py.exc.WebSocketException
Bases: ws4py.exc.WebSocketException
Bases: ws4py.exc.WebSocketException
Bases: ws4py.exc.WebSocketException
Bases: ws4py.exc.WebSocketException
Bases: object
Implements 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
Builds a frame from the instance’s attributes and returns its bytes representation.
Performs the masking or unmasking operation on data using the simple masking algorithm:
Performs the masking or unmasking operation on data using the simple masking algorithm:
The manager module provides a selected classes to handle websocket’s execution.
Initially the rationale was to:
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.
Bases: object
A socket poller that uses the select implementation to determines which file descriptors have data available to read.
It is available on all platforms.
Bases: object
An epoll poller that uses the epoll implementation to determines which file descriptors have data available to read.
Available on Unix flavors mostly.
Bases: threading.Thread
An 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 epoll if available else select unless you provide your own poller.
Manage a new websocket.
First calls its opened() method and register its socket against the poller for reading events.
Remove the given websocket from 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.
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, its terminate() 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.
Bases: object
A 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:
The opcode indicates the message type and data is the possible message payload.
The payload is held internally as a a bytearray as they are faster than pure strings for append operations.
Unicode data will be encoded using the provided encoding.
Returns a frame bytes with the fin bit set and a random mask.
If mask is set, automatically mask the frame using a generated 4-byte token.
Returns a ws4py.framing.Frame bytes.
The behavior depends on the given flags:
Bases: ws4py.messaging.Message
Bases: ws4py.messaging.Message
Bases: ws4py.messaging.Message
Bases: ws4py.messaging.Message
Bases: ws4py.messaging.Message
Bases: object
Represents 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_mask to mask all frames built.
Set expect_masking to indicate masking will be checked on all parsed frames.
Parsed test or binary messages. Whenever the parser reads more bytes from a fragment message, those bytes are appended to the most recent message.
Parsed ping control messages. They are instances of ws4py.messaging.PingControlMessage
Parsed pong control messages. They are instances of ws4py.messaging.PongControlMessage
Parsed close control messsage. Instance of ws4py.messaging.CloseControlMessage
Detected errors while parsing. Instances of ws4py.messaging.CloseControlMessage
Returns a ws4py.messaging.TextMessage instance ready to be built. Convenience method so that the caller doesn’t need to import the ws4py.messaging.TextMessage class itself.
Returns a ws4py.messaging.BinaryMessage instance ready to be built. Convenience method so that the caller doesn’t need to import the ws4py.messaging.BinaryMessage class itself.
Checks if the stream has received any message which, if fragmented, is now completed.
Returns a close control message built from a ws4py.messaging.CloseControlMessage instance, using the given status code and reason message.
Returns a ping control message built from a ws4py.messaging.PingControlMessage instance.
Returns a ping control message built from a ws4py.messaging.PongControlMessage instance.
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.Frame object, 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.
Bases: object
Incremental 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/).
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.
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.
Bases: threading.Thread
Runs 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.
Bases: object
The sock is an opened connection resulting from the websocket handshake.
If protocols is provided, it is a list of protocols negotiated during the handshake as is extensions.
If environ is provided, it is a copy of the WSGI environ dictionnary from the underlying WSGI server.
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_mask fields to True and stream.expect_masking fields to False.
List of protocols supported by this endpoint. Unused for now.
List of extensions supported by this endpoint. Unused for now.
Underlying connection.
Indicates if the client has been marked as terminated.
Indicates if the server has been marked as terminated.
Current connection reading buffer size.
WSGI environ dictionary.
At which interval the heartbeat will be running. Set this to 0 or None to disable it entirely.
Call this method to initiate the websocket connection closing by sending a close frame to the connected peer. The code is the status code representing the termination’s reason.
Once this method is called, the server_terminated attribute 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
Called when the websocket stream and connection are finally closed. The provided code is status set by the other point and reason is a human readable message.
See also
Defined Status Codes http://tools.ietf.org/html/rfc6455#section-7.4.1
Pong message, as a messaging.PongControlMessage instance, received on the stream.
Called whenever a complete message, binary or text, is received and ready for application’s processing.
The passed message is an instance of messaging.TextMessage or messaging.BinaryMessage.
Note
You should override this method in your subclass.
Sends the given payload out.
If payload is some bytes or a bytearray, then it is sent as a single message not fragmented.
If payload is a generator, each chunk is sent as part of fragmented message.
If binary is set, handles the payload as a binary message.
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.
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.
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:
The process should be terminated when this method returns False.
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:
This method is blocking and should likely be run in a thread.
Bases: ws4py.websocket.WebSocket
The sock is an opened connection resulting from the websocket handshake.
If protocols is provided, it is a list of protocols negotiated during the handshake as is extensions.
If environ is provided, it is a copy of the WSGI environ dictionnary from the underlying WSGI server.