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, socket=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, socket=None)[source]
WebSocketApp initialization
- Parameters:
url (str) – Websocket url.
header (list or dict) – Custom header for websocket handshake.
on_open (function) – Callback object which is called at opening websocket. on_open has one argument. The 1st argument is this class object.
on_message (function) – Callback 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 data received from the server.
on_error (function) – Callback 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 (function) – Callback object which is called when connection is closed. on_close has 3 arguments. The 1st argument is this class object. The 2nd argument is close_status_code. The 3rd argument is close_msg.
on_cont_message (function) – Callback object which is called when a continuation frame is received. 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 (function) – 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 (bool) – This parameter is obsolete and ignored.
get_mask_key (function) – A callable function to get new mask keys, see the WebSocket.set_mask_key’s docstring for more information.
cookie (str) – Cookie value.
subprotocols (list) – List of available sub protocols. Default is None.
socket (socket) – Pre-initialized stream socket.
- 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, http_proxy_timeout=None, skip_utf8_validation=False, host=None, origin=None, dispatcher=None, suppress_origin=False, proxy_type=None, reconnect=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, no ping is sent periodically.
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 (str) – HTTP proxy host name.
http_proxy_port (int or str) – HTTP proxy port. If not set, set to 80.
http_no_proxy (list) – Whitelisted host names that don’t use the proxy.
http_proxy_timeout (int or float) – HTTP proxy timeout, default is 60 sec as per python-socks.
http_proxy_auth (tuple) – HTTP proxy auth information. tuple of username and password. Default is None.
skip_utf8_validation (bool) – skip utf8 validation.
host (str) – update host header.
origin (str) – update origin header.
dispatcher (Dispatcher object) – customize reading data from socket.
suppress_origin (bool) – suppress outputting origin header.
proxy_type (str) – type of proxy from: http, socks4, socks4a, socks5, socks5h
reconnect (int) – delay interval when reconnecting
- Returns:
teardown – False if the WebSocketApp is closed or caught KeyboardInterrupt, True if any other exception was raised during a loop.
- Return type:
bool