WebSocketBaseClient(url, protocols=None, extensions=None, heartbeat_freq=None, ssl_options=None, headers=None, exclude_headers=None)¶
A websocket client that implements RFC 6455 and provides a simple interface to communicate with a websocket server.
This class works on its own but will block if not run in its own thread.
When an instance of this class is created, a
socketis created. If the connection is a TCP socket, the nagle’s algorithm is disabled.
The address of the server will be extracted from the given websocket url.
The websocket key is randomly generated, reset the key attribute if you want to provide yours.
For instance to create a TCP client:
>>> from ws4py.client import WebSocketBaseClient >>> ws = WebSocketBaseClient('ws://localhost/ws')
Here is an example for a TCP client over SSL:
>>> from ws4py.client import WebSocketBaseClient >>> ws = WebSocketBaseClient('wss://localhost/ws')
Finally an example of a Unix-domain connection:
>>> from ws4py.client import WebSocketBaseClient >>> ws = WebSocketBaseClient('ws+unix:///tmp/my.sock')
Note that in this case, the initial Upgrade request will be sent to
/. You may need to change this by setting the resource explicitely before connecting:
>>> from ws4py.client import WebSocketBaseClient >>> ws = WebSocketBaseClient('ws+unix:///tmp/my.sock') >>> ws.resource = '/ws' >>> ws.connect()
You may provide extra headers by passing a list of tuples which must be unicode objects.
Returns the Unix socket path if or a tuple
(host, port)depending on the initial URL’s scheme.
Initiate the closing handshake with the server.
Connects this websocket and starts the upgrade handshake with the remote endpoint.
List of headers appropriate for the upgrade handshake.
Prepare the request to be sent for the upgrade handshake.
Ensure that we received a HTTP 101 status code in response to our request and if not raises
Read the upgrade handshake’s response headers and validate them against RFC 6455.
WebSocketClient(url, protocols=None, extensions=None, heartbeat_freq=None, ssl_options=None, headers=None, exclude_headers=None)¶
from ws4py.client.threadedclient import WebSocketClient class EchoClient(WebSocketClient): def opened(self): for i in range(0, 200, 25): self.send("*" * i) def closed(self, code, reason): print(("Closed down", code, reason)) def received_message(self, m): print("=> %d %s" % (len(m), str(m))) try: ws = EchoClient('ws://localhost:9000/echo', protocols=['http-only', 'chat']) ws.connect() except KeyboardInterrupt: ws.close()
True if the client’s thread is set to be a daemon thread.
Simply blocks the thread until the websocket has terminated.
Called when the upgrade handshake has completed successfully.
Starts the client’s thread.