client Package

client Package

class ws4py.client.WebSocketBaseClient(url, protocols=None, extensions=None, heartbeat_freq=None, ssl_options=None, headers=None)[source]

Bases: ws4py.websocket.WebSocket

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 socket is 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 websocket.client import WebSocketBaseClient
>>> ws = WebSocketBaseClient('ws://localhost/ws')

Here is an example for a TCP client over SSL:

>>> from websocket.client import WebSocketBaseClient
>>> ws = WebSocketBaseClient('wss://localhost/ws')

Finally an example of a Unix-domain connection:

>>> from websocket.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 websocket.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.

bind_addr

Returns the Unix socket path if or a tuple (host, port) depending on the initial URL’s scheme.

close(code=1000, reason='')[source]

Initiate the closing handshake with the server.

connect()[source]

Connects this websocket and starts the upgrade handshake with the remote endpoint.

handshake_headers

List of headers appropriate for the upgrade handshake.

handshake_request

Prepare the request to be sent for the upgrade handshake.

process_response_line(response_line)[source]

Ensure that we received a HTTP 101 status code in response to our request and if not raises HandshakeError.

process_handshake_header(headers)[source]

Read the upgrade handshake’s response headers and validate them against RFC 6455.

handshake_ok()[source]

geventclient Module

threadedclient Module

class ws4py.client.threadedclient.WebSocketClient(url, protocols=None, extensions=None, heartbeat_freq=None, ssl_options=None, headers=None)[source]

Bases: ws4py.client.WebSocketBaseClient

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()
daemon

True if the client’s thread is set to be a daemon thread.

run_forever()[source]

Simply blocks the thread until the websocket has terminated.

handshake_ok()[source]

Called when the upgrade handshake has completed successfully.

Starts the client’s thread.

tornadoclient Module

class ws4py.client.tornadoclient.TornadoWebSocketClient(url, protocols=None, extensions=None, io_loop=None, ssl_options=None, headers=None)[source]

Bases: ws4py.client.WebSocketBaseClient

from tornado import ioloop

class MyClient(TornadoWebSocketClient):
    def opened(self):
        for i in range(0, 200, 25):
            self.send("*" * i)

    def received_message(self, m):
        print((m, len(str(m))))

    def closed(self, code, reason=None):
        ioloop.IOLoop.instance().stop()

ws = MyClient('ws://localhost:9000/echo', protocols=['http-only', 'chat'])
ws.connect()

ioloop.IOLoop.instance().start()
connect()[source]

Connects the websocket and initiate the upgrade handshake.

close_connection()[source]

Close the underlying connection