2021-05-18 03:18:25 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2018-10-31 16:08:29 +00:00
|
|
|
"""
|
2022-01-24 04:07:52 +00:00
|
|
|
_app.py
|
2018-10-31 16:08:29 +00:00
|
|
|
websocket - WebSocket client library for Python
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
Copyright 2021 engn33r
|
2018-10-31 16:08:29 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2018-10-31 16:08:29 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2018-10-31 16:08:29 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2018-10-31 16:08:29 +00:00
|
|
|
"""
|
2021-05-18 18:33:37 +00:00
|
|
|
import selectors
|
2018-10-31 16:08:29 +00:00
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
import traceback
|
|
|
|
from ._abnf import ABNF
|
|
|
|
from ._core import WebSocket, getdefaulttimeout
|
|
|
|
from ._exceptions import *
|
|
|
|
from . import _logging
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ["WebSocketApp"]
|
|
|
|
|
2021-05-18 03:18:25 +00:00
|
|
|
|
2021-05-08 14:39:00 +00:00
|
|
|
class Dispatcher:
|
2021-05-18 03:18:25 +00:00
|
|
|
"""
|
|
|
|
Dispatcher
|
|
|
|
"""
|
2021-05-08 14:39:00 +00:00
|
|
|
def __init__(self, app, ping_timeout):
|
2021-05-18 03:18:25 +00:00
|
|
|
self.app = app
|
2021-05-08 14:39:00 +00:00
|
|
|
self.ping_timeout = ping_timeout
|
|
|
|
|
|
|
|
def read(self, sock, read_callback, check_callback):
|
2021-05-18 03:18:25 +00:00
|
|
|
while self.app.keep_running:
|
2021-05-18 18:33:37 +00:00
|
|
|
sel = selectors.DefaultSelector()
|
|
|
|
sel.register(self.app.sock.sock, selectors.EVENT_READ)
|
|
|
|
|
|
|
|
r = sel.select(self.ping_timeout)
|
2021-05-08 14:39:00 +00:00
|
|
|
if r:
|
|
|
|
if not read_callback():
|
|
|
|
break
|
|
|
|
check_callback()
|
2021-05-18 18:33:37 +00:00
|
|
|
sel.close()
|
2021-05-08 14:39:00 +00:00
|
|
|
|
2021-05-18 03:18:25 +00:00
|
|
|
|
|
|
|
class SSLDispatcher:
|
|
|
|
"""
|
|
|
|
SSLDispatcher
|
|
|
|
"""
|
2021-05-08 14:39:00 +00:00
|
|
|
def __init__(self, app, ping_timeout):
|
2021-05-18 03:18:25 +00:00
|
|
|
self.app = app
|
2021-05-08 14:39:00 +00:00
|
|
|
self.ping_timeout = ping_timeout
|
|
|
|
|
|
|
|
def read(self, sock, read_callback, check_callback):
|
2021-05-18 03:18:25 +00:00
|
|
|
while self.app.keep_running:
|
2021-05-08 14:39:00 +00:00
|
|
|
r = self.select()
|
|
|
|
if r:
|
|
|
|
if not read_callback():
|
|
|
|
break
|
|
|
|
check_callback()
|
|
|
|
|
|
|
|
def select(self):
|
|
|
|
sock = self.app.sock.sock
|
|
|
|
if sock.pending():
|
|
|
|
return [sock,]
|
|
|
|
|
2021-05-18 18:33:37 +00:00
|
|
|
sel = selectors.DefaultSelector()
|
|
|
|
sel.register(sock, selectors.EVENT_READ)
|
|
|
|
|
|
|
|
r = sel.select(self.ping_timeout)
|
|
|
|
sel.close()
|
|
|
|
|
|
|
|
if len(r) > 0:
|
|
|
|
return r[0][0]
|
2018-10-31 16:08:29 +00:00
|
|
|
|
2021-05-18 03:18:25 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
class WebSocketApp:
|
2018-10-31 16:08:29 +00:00
|
|
|
"""
|
2021-05-18 03:18:25 +00:00
|
|
|
Higher level of APIs are provided. The interface is like JavaScript WebSocket object.
|
2018-10-31 16:08:29 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, 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):
|
|
|
|
"""
|
2021-05-18 03:18:25 +00:00
|
|
|
WebSocketApp initialization
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-01-24 04:07:52 +00:00
|
|
|
url: str
|
|
|
|
Websocket url.
|
2021-05-18 03:18:25 +00:00
|
|
|
header: list or dict
|
2022-01-24 04:07:52 +00:00
|
|
|
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.
|
2021-05-18 03:18:25 +00:00
|
|
|
on_message has 2 arguments.
|
|
|
|
The 1st argument is this class object.
|
2022-01-24 04:07:52 +00:00
|
|
|
The 2nd argument is utf-8 data received from the server.
|
|
|
|
on_error: function
|
|
|
|
Callback object which is called when we get error.
|
2021-05-18 03:18:25 +00:00
|
|
|
on_error has 2 arguments.
|
|
|
|
The 1st argument is this class object.
|
|
|
|
The 2nd argument is exception object.
|
2022-01-24 04:07:52 +00:00
|
|
|
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.
|
2021-05-18 03:18:25 +00:00
|
|
|
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
|
2022-01-24 04:07:52 +00:00
|
|
|
on_data: function
|
|
|
|
Callback object which is called when a message received.
|
2021-05-18 03:18:25 +00:00
|
|
|
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.
|
2022-01-24 04:07:52 +00:00
|
|
|
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.
|
2021-05-18 03:18:25 +00:00
|
|
|
cookie: str
|
2022-01-24 04:07:52 +00:00
|
|
|
Cookie value.
|
|
|
|
subprotocols: list
|
|
|
|
List of available sub protocols. Default is None.
|
2018-10-31 16:08:29 +00:00
|
|
|
"""
|
|
|
|
self.url = url
|
|
|
|
self.header = header if header is not None else []
|
|
|
|
self.cookie = cookie
|
2021-05-08 14:39:00 +00:00
|
|
|
|
2018-10-31 16:08:29 +00:00
|
|
|
self.on_open = on_open
|
|
|
|
self.on_message = on_message
|
|
|
|
self.on_data = on_data
|
|
|
|
self.on_error = on_error
|
|
|
|
self.on_close = on_close
|
|
|
|
self.on_ping = on_ping
|
|
|
|
self.on_pong = on_pong
|
|
|
|
self.on_cont_message = on_cont_message
|
2021-05-08 14:39:00 +00:00
|
|
|
self.keep_running = False
|
2018-10-31 16:08:29 +00:00
|
|
|
self.get_mask_key = get_mask_key
|
|
|
|
self.sock = None
|
|
|
|
self.last_ping_tm = 0
|
|
|
|
self.last_pong_tm = 0
|
|
|
|
self.subprotocols = subprotocols
|
|
|
|
|
|
|
|
def send(self, data, opcode=ABNF.OPCODE_TEXT):
|
|
|
|
"""
|
2021-05-18 03:18:25 +00:00
|
|
|
send message
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-01-24 04:07:52 +00:00
|
|
|
data: str
|
2021-05-18 03:18:25 +00:00
|
|
|
Message to send. If you set opcode to OPCODE_TEXT,
|
|
|
|
data must be utf-8 string or unicode.
|
2022-01-24 04:07:52 +00:00
|
|
|
opcode: int
|
|
|
|
Operation code of data. Default is OPCODE_TEXT.
|
2018-10-31 16:08:29 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
if not self.sock or self.sock.send(data, opcode) == 0:
|
|
|
|
raise WebSocketConnectionClosedException(
|
|
|
|
"Connection is already closed.")
|
|
|
|
|
|
|
|
def close(self, **kwargs):
|
|
|
|
"""
|
2021-05-18 03:18:25 +00:00
|
|
|
Close websocket connection.
|
2018-10-31 16:08:29 +00:00
|
|
|
"""
|
|
|
|
self.keep_running = False
|
|
|
|
if self.sock:
|
|
|
|
self.sock.close(**kwargs)
|
2021-05-08 14:39:00 +00:00
|
|
|
self.sock = None
|
2018-10-31 16:08:29 +00:00
|
|
|
|
2021-05-18 03:18:25 +00:00
|
|
|
def _send_ping(self, interval, event, payload):
|
2018-10-31 16:08:29 +00:00
|
|
|
while not event.wait(interval):
|
|
|
|
self.last_ping_tm = time.time()
|
|
|
|
if self.sock:
|
|
|
|
try:
|
2021-05-18 03:18:25 +00:00
|
|
|
self.sock.ping(payload)
|
2018-10-31 16:08:29 +00:00
|
|
|
except Exception as ex:
|
|
|
|
_logging.warning("send_ping routine terminated: {}".format(ex))
|
|
|
|
break
|
|
|
|
|
|
|
|
def run_forever(self, sockopt=None, sslopt=None,
|
|
|
|
ping_interval=0, ping_timeout=None,
|
2021-05-18 03:18:25 +00:00
|
|
|
ping_payload="",
|
2018-10-31 16:08:29 +00:00
|
|
|
http_proxy_host=None, http_proxy_port=None,
|
|
|
|
http_no_proxy=None, http_proxy_auth=None,
|
|
|
|
skip_utf8_validation=False,
|
2021-05-08 14:39:00 +00:00
|
|
|
host=None, origin=None, dispatcher=None,
|
2021-05-18 03:18:25 +00:00
|
|
|
suppress_origin=False, proxy_type=None):
|
2018-10-31 16:08:29 +00:00
|
|
|
"""
|
2021-05-18 03:18:25 +00:00
|
|
|
Run event loop for WebSocket framework.
|
|
|
|
|
|
|
|
This loop is an infinite loop and is alive while websocket is available.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
sockopt: tuple
|
2022-01-24 04:07:52 +00:00
|
|
|
Values for socket.setsockopt.
|
2018-10-31 16:08:29 +00:00
|
|
|
sockopt must be tuple
|
|
|
|
and each element is argument of sock.setsockopt.
|
2021-05-18 03:18:25 +00:00
|
|
|
sslopt: dict
|
2022-01-24 04:07:52 +00:00
|
|
|
Optional dict object for ssl socket option.
|
2021-05-18 03:18:25 +00:00
|
|
|
ping_interval: int or float
|
2022-01-24 04:07:52 +00:00
|
|
|
Automatically send "ping" command
|
|
|
|
every specified period (in seconds).
|
|
|
|
If set to 0, no ping is sent periodically.
|
2021-05-18 03:18:25 +00:00
|
|
|
ping_timeout: int or float
|
2022-01-24 04:07:52 +00:00
|
|
|
Timeout (in seconds) if the pong message is not received.
|
2021-05-18 03:18:25 +00:00
|
|
|
ping_payload: str
|
2022-01-24 04:07:52 +00:00
|
|
|
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.
|
2021-05-18 03:18:25 +00:00
|
|
|
skip_utf8_validation: bool
|
|
|
|
skip utf8 validation.
|
|
|
|
host: str
|
|
|
|
update host header.
|
|
|
|
origin: str
|
|
|
|
update origin header.
|
2022-01-24 04:07:52 +00:00
|
|
|
dispatcher: Dispatcher object
|
2021-05-18 03:18:25 +00:00
|
|
|
customize reading data from socket.
|
|
|
|
suppress_origin: bool
|
|
|
|
suppress outputting origin header.
|
2021-05-08 14:39:00 +00:00
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2021-05-18 03:18:25 +00:00
|
|
|
teardown: bool
|
|
|
|
False if caught KeyboardInterrupt, True if other exception was raised during a loop
|
2018-10-31 16:08:29 +00:00
|
|
|
"""
|
|
|
|
|
2021-05-08 14:39:00 +00:00
|
|
|
if ping_timeout is not None and ping_timeout <= 0:
|
2021-05-18 18:33:37 +00:00
|
|
|
raise WebSocketException("Ensure ping_timeout > 0")
|
|
|
|
if ping_interval is not None and ping_interval < 0:
|
|
|
|
raise WebSocketException("Ensure ping_interval >= 0")
|
2018-10-31 16:08:29 +00:00
|
|
|
if ping_timeout and ping_interval and ping_interval <= ping_timeout:
|
|
|
|
raise WebSocketException("Ensure ping_interval > ping_timeout")
|
2021-05-08 14:39:00 +00:00
|
|
|
if not sockopt:
|
2018-10-31 16:08:29 +00:00
|
|
|
sockopt = []
|
2021-05-08 14:39:00 +00:00
|
|
|
if not sslopt:
|
2018-10-31 16:08:29 +00:00
|
|
|
sslopt = {}
|
|
|
|
if self.sock:
|
|
|
|
raise WebSocketException("socket is already opened")
|
|
|
|
thread = None
|
2021-05-08 14:39:00 +00:00
|
|
|
self.keep_running = True
|
|
|
|
self.last_ping_tm = 0
|
|
|
|
self.last_pong_tm = 0
|
|
|
|
|
|
|
|
def teardown(close_frame=None):
|
|
|
|
"""
|
|
|
|
Tears down the connection.
|
2021-05-18 03:18:25 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
close_frame: ABNF frame
|
|
|
|
If close_frame is set, the on_close handler is invoked
|
|
|
|
with the statusCode and reason from the provided frame.
|
2021-05-08 14:39:00 +00:00
|
|
|
"""
|
2021-05-18 18:33:37 +00:00
|
|
|
|
2021-05-18 03:18:25 +00:00
|
|
|
if thread and thread.is_alive():
|
2021-05-08 14:39:00 +00:00
|
|
|
event.set()
|
|
|
|
thread.join()
|
|
|
|
self.keep_running = False
|
|
|
|
if self.sock:
|
|
|
|
self.sock.close()
|
2021-05-18 18:33:37 +00:00
|
|
|
close_status_code, close_reason = self._get_close_args(
|
|
|
|
close_frame if close_frame else None)
|
2021-05-08 14:39:00 +00:00
|
|
|
self.sock = None
|
2018-10-31 16:08:29 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
# Finally call the callback AFTER all teardown is complete
|
|
|
|
self._callback(self.on_close, close_status_code, close_reason)
|
|
|
|
|
2018-10-31 16:08:29 +00:00
|
|
|
try:
|
|
|
|
self.sock = WebSocket(
|
|
|
|
self.get_mask_key, sockopt=sockopt, sslopt=sslopt,
|
2021-05-08 14:39:00 +00:00
|
|
|
fire_cont_frame=self.on_cont_message is not None,
|
|
|
|
skip_utf8_validation=skip_utf8_validation,
|
2022-01-24 04:07:52 +00:00
|
|
|
enable_multithread=True)
|
2018-10-31 16:08:29 +00:00
|
|
|
self.sock.settimeout(getdefaulttimeout())
|
|
|
|
self.sock.connect(
|
|
|
|
self.url, header=self.header, cookie=self.cookie,
|
|
|
|
http_proxy_host=http_proxy_host,
|
|
|
|
http_proxy_port=http_proxy_port, http_no_proxy=http_no_proxy,
|
|
|
|
http_proxy_auth=http_proxy_auth, subprotocols=self.subprotocols,
|
2021-05-08 14:39:00 +00:00
|
|
|
host=host, origin=origin, suppress_origin=suppress_origin,
|
|
|
|
proxy_type=proxy_type)
|
|
|
|
if not dispatcher:
|
|
|
|
dispatcher = self.create_dispatcher(ping_timeout)
|
|
|
|
|
2018-10-31 16:08:29 +00:00
|
|
|
self._callback(self.on_open)
|
|
|
|
|
|
|
|
if ping_interval:
|
|
|
|
event = threading.Event()
|
|
|
|
thread = threading.Thread(
|
2021-05-18 03:18:25 +00:00
|
|
|
target=self._send_ping, args=(ping_interval, event, ping_payload))
|
|
|
|
thread.daemon = True
|
2018-10-31 16:08:29 +00:00
|
|
|
thread.start()
|
|
|
|
|
2021-05-08 14:39:00 +00:00
|
|
|
def read():
|
2018-10-31 16:08:29 +00:00
|
|
|
if not self.keep_running:
|
2021-05-08 14:39:00 +00:00
|
|
|
return teardown()
|
2018-10-31 16:08:29 +00:00
|
|
|
|
2021-05-08 14:39:00 +00:00
|
|
|
op_code, frame = self.sock.recv_data_frame(True)
|
|
|
|
if op_code == ABNF.OPCODE_CLOSE:
|
|
|
|
return teardown(frame)
|
|
|
|
elif op_code == ABNF.OPCODE_PING:
|
|
|
|
self._callback(self.on_ping, frame.data)
|
|
|
|
elif op_code == ABNF.OPCODE_PONG:
|
|
|
|
self.last_pong_tm = time.time()
|
|
|
|
self._callback(self.on_pong, frame.data)
|
|
|
|
elif op_code == ABNF.OPCODE_CONT and self.on_cont_message:
|
|
|
|
self._callback(self.on_data, frame.data,
|
|
|
|
frame.opcode, frame.fin)
|
|
|
|
self._callback(self.on_cont_message,
|
|
|
|
frame.data, frame.fin)
|
|
|
|
else:
|
|
|
|
data = frame.data
|
2021-05-18 18:33:37 +00:00
|
|
|
if op_code == ABNF.OPCODE_TEXT:
|
2021-05-08 14:39:00 +00:00
|
|
|
data = data.decode("utf-8")
|
|
|
|
self._callback(self.on_data, data, frame.opcode, True)
|
|
|
|
self._callback(self.on_message, data)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def check():
|
|
|
|
if (ping_timeout):
|
|
|
|
has_timeout_expired = time.time() - self.last_ping_tm > ping_timeout
|
|
|
|
has_pong_not_arrived_after_last_ping = self.last_pong_tm - self.last_ping_tm < 0
|
|
|
|
has_pong_arrived_too_late = self.last_pong_tm - self.last_ping_tm > ping_timeout
|
|
|
|
|
2021-05-18 03:18:25 +00:00
|
|
|
if (self.last_ping_tm and
|
|
|
|
has_timeout_expired and
|
|
|
|
(has_pong_not_arrived_after_last_ping or has_pong_arrived_too_late)):
|
2021-05-08 14:39:00 +00:00
|
|
|
raise WebSocketTimeoutException("ping/pong timed out")
|
|
|
|
return True
|
|
|
|
|
|
|
|
dispatcher.read(self.sock.sock, read, check)
|
2018-10-31 16:08:29 +00:00
|
|
|
except (Exception, KeyboardInterrupt, SystemExit) as e:
|
|
|
|
self._callback(self.on_error, e)
|
|
|
|
if isinstance(e, SystemExit):
|
|
|
|
# propagate SystemExit further
|
|
|
|
raise
|
2021-05-08 14:39:00 +00:00
|
|
|
teardown()
|
|
|
|
return not isinstance(e, KeyboardInterrupt)
|
|
|
|
|
|
|
|
def create_dispatcher(self, ping_timeout):
|
|
|
|
timeout = ping_timeout or 10
|
|
|
|
if self.sock.is_ssl():
|
2021-05-18 03:18:25 +00:00
|
|
|
return SSLDispatcher(self, timeout)
|
2021-05-08 14:39:00 +00:00
|
|
|
|
|
|
|
return Dispatcher(self, timeout)
|
2018-10-31 16:08:29 +00:00
|
|
|
|
2021-05-18 18:33:37 +00:00
|
|
|
def _get_close_args(self, close_frame):
|
2021-05-18 03:18:25 +00:00
|
|
|
"""
|
2021-05-18 18:33:37 +00:00
|
|
|
_get_close_args extracts the close code and reason from the close body
|
|
|
|
if it exists (RFC6455 says WebSocket Connection Close Code is optional)
|
2021-05-18 03:18:25 +00:00
|
|
|
"""
|
2021-05-18 18:33:37 +00:00
|
|
|
# Need to catch the case where close_frame is None
|
|
|
|
# Otherwise the following if statement causes an error
|
|
|
|
if not self.on_close or not close_frame:
|
|
|
|
return [None, None]
|
|
|
|
|
|
|
|
# Extract close frame status code
|
|
|
|
if close_frame.data and len(close_frame.data) >= 2:
|
|
|
|
close_status_code = 256 * close_frame.data[0] + close_frame.data[1]
|
|
|
|
reason = close_frame.data[2:].decode('utf-8')
|
|
|
|
return [close_status_code, reason]
|
2018-10-31 16:08:29 +00:00
|
|
|
else:
|
2021-05-18 18:33:37 +00:00
|
|
|
# Most likely reached this because len(close_frame_data.data) < 2
|
|
|
|
return [None, None]
|
2018-10-31 16:08:29 +00:00
|
|
|
|
|
|
|
def _callback(self, callback, *args):
|
|
|
|
if callback:
|
|
|
|
try:
|
2021-05-18 18:33:37 +00:00
|
|
|
callback(self, *args)
|
2021-05-08 14:39:00 +00:00
|
|
|
|
2018-10-31 16:08:29 +00:00
|
|
|
except Exception as e:
|
|
|
|
_logging.error("error from callback {}: {}".format(callback, e))
|
2022-01-24 04:07:52 +00:00
|
|
|
if self.on_error:
|
|
|
|
self.on_error(self, e)
|