websocket/_app.py

The _app.py file

class websocket._app.WebSocketApp(url, header=None, on_open=None, on_message=None, on_error=None, on_close=None, on_ping=None, on_pong=None, on_cont_message=None, keep_running=True, get_mask_key=None, cookie=None, subprotocols=None, on_data=None)[source]

Higher level of APIs are provided. The interface is like JavaScript WebSocket object.

__init__(url, header=None, on_open=None, on_message=None, on_error=None, on_close=None, on_ping=None, on_pong=None, on_cont_message=None, keep_running=True, get_mask_key=None, cookie=None, subprotocols=None, on_data=None)[source]

WebSocketApp initialization

Parameters
  • url (<type>) – websocket url.

  • header (list or dict) – custom header for websocket handshake.

  • on_open (<type>) – callable object which is called at opening websocket. this function has one argument. The argument is this class object.

  • on_message (<type>) – callable object which is called when received data. on_message has 2 arguments. The 1st argument is this class object. The 2nd argument is utf-8 string which we get from the server.

  • on_error (<type>) – callable object which is called when we get error. on_error has 2 arguments. The 1st argument is this class object. The 2nd argument is exception object.

  • on_close (<type>) – callable object which is called when closed the connection. this function has one argument. The argument is this class object.

  • on_cont_message (<type>) – callback object which is called when receive continued frame data. on_cont_message has 3 arguments. The 1st argument is this class object. The 2nd argument is utf-8 string which we get from the server. The 3rd argument is continue flag. if 0, the data continue to next frame data

  • on_data (<type>) – callback object which is called when a message received. This is called before on_message or on_cont_message, and then on_message or on_cont_message is called. on_data has 4 argument. The 1st argument is this class object. The 2nd argument is utf-8 string which we get from the server. The 3rd argument is data type. ABNF.OPCODE_TEXT or ABNF.OPCODE_BINARY will be came. The 4th argument is continue flag. if 0, the data continue

  • keep_running (<type>) – this parameter is obsolete and ignored.

  • get_mask_key (func) – a callable to produce new mask keys, see the WebSocket.set_mask_key’s docstring for more information

  • cookie (str) – cookie value.

  • subprotocols (<type>) – array of available sub protocols. default is None.

close(**kwargs)[source]

Close websocket connection.

run_forever(sockopt=None, sslopt=None, ping_interval=0, ping_timeout=None, ping_payload='', http_proxy_host=None, http_proxy_port=None, http_no_proxy=None, http_proxy_auth=None, skip_utf8_validation=False, host=None, origin=None, dispatcher=None, suppress_origin=False, proxy_type=None)[source]

Run event loop for WebSocket framework.

This loop is an infinite loop and is alive while websocket is available.

Parameters
  • sockopt (tuple) – values for socket.setsockopt. sockopt must be tuple and each element is argument of sock.setsockopt.

  • sslopt (dict) – optional dict object for ssl socket option.

  • ping_interval (int or float) – automatically send “ping” command every specified period (in seconds) if set to 0, not send automatically.

  • ping_timeout (int or float) – timeout (in seconds) if the pong message is not received.

  • ping_payload (str) – payload message to send with each ping.

  • http_proxy_host (<type>) – http proxy host name.

  • http_proxy_port (<type>) – http proxy port. If not set, set to 80.

  • http_no_proxy (<type>) – host names, which doesn’t use proxy.

  • skip_utf8_validation (bool) – skip utf8 validation.

  • host (str) – update host header.

  • origin (str) – update origin header.

  • dispatcher (<type>) – customize reading data from socket.

  • suppress_origin (bool) – suppress outputting origin header.

Returns

teardown – False if caught KeyboardInterrupt, True if other exception was raised during a loop

Return type

bool

send(data, opcode=1)[source]

send message

Parameters
  • data (<type>) – Message to send. If you set opcode to OPCODE_TEXT, data must be utf-8 string or unicode.

  • opcode (<type>) – Operation code of data. default is OPCODE_TEXT.