本文整理汇总了Python中websocket.WebSocketException方法的典型用法代码示例。如果您正苦于以下问题:Python websocket.WebSocketException方法的具体用法?Python websocket.WebSocketException怎么用?Python websocket.WebSocketException使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类websocket
的用法示例。
在下文中一共展示了websocket.WebSocketException方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testRecvWithFireEventOfFragmentation
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def testRecvWithFireEventOfFragmentation(self):
sock = ws.WebSocket(fire_cont_frame=True)
s = sock.sock = SockMock()
# OPCODE=TEXT, FIN=0, MSG="Brevity is "
s.add_packet(six.b("\x01\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C"))
# OPCODE=CONT, FIN=0, MSG="Brevity is "
s.add_packet(six.b("\x00\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C"))
# OPCODE=CONT, FIN=1, MSG="the soul of wit"
s.add_packet(six.b("\x80\x8fabcd\x15\n\x06D\x12\r\x16\x08A\r\x05D\x16\x0b\x17"))
_, data = sock.recv_data()
self.assertEqual(data, six.b("Brevity is "))
_, data = sock.recv_data()
self.assertEqual(data, six.b("Brevity is "))
_, data = sock.recv_data()
self.assertEqual(data, six.b("the soul of wit"))
# OPCODE=CONT, FIN=0, MSG="Brevity is "
s.add_packet(six.b("\x80\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C"))
with self.assertRaises(ws.WebSocketException):
sock.recv_data()
with self.assertRaises(ws.WebSocketConnectionClosedException):
sock.recv()
示例2: recv
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def recv(self, ws):
try:
frame = ws.recv_frame()
except websocket.WebSocketException:
return websocket.ABNF.OPCODE_CLOSE, None
if not frame:
raise websocket.WebSocketException("Not a valid frame %s" % frame)
elif frame.opcode in OPCODE_DATA:
return frame.opcode, frame.data
elif frame.opcode == websocket.ABNF.OPCODE_CLOSE:
ws.send_close()
return frame.opcode, None
elif frame.opcode == websocket.ABNF.OPCODE_PING:
ws.pong(frame.data)
return frame.opcode, frame.data
return frame.opcode, frame.data
示例3: abort_query
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def abort_query(self):
"""
Abort running query
This function should be called from a separate thread and has no response
Response should be checked in the main thread which started execution of query
There are three possible outcomes of calling this function:
1) Query is aborted normally, connection remains active
2) Query was stuck in a state which cannot be aborted, so Exasol has to terminate connection
3) Query might be finished successfully before abort call had a chance to take effect
"""
req = {
'command': 'abortQuery'
}
send_data = self.json_encode(req)
self.logger.debug_json('WebSocket abort request', req)
try:
self._ws_send(send_data)
except (websocket.WebSocketException, ConnectionError) as e:
self.close(disconnect=False)
raise ExaCommunicationError(self, str(e))
示例4: receive
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def receive(ws):
frame = ws.recv_frame()
if not frame:
raise websocket.WebSocketException("Not a valid frame %s" % frame)
elif frame.opcode in opcode_data:
return frame.opcode, frame.data
elif frame.opcode == websocket.ABNF.OPCODE_CLOSE:
ws.send_close()
return frame.opcode, None
elif frame.opcode == websocket.ABNF.OPCODE_PING:
# logger.debug("Tautulli WebSocket :: Received ping, sending pong.")
ws.pong("Hi!")
elif frame.opcode == websocket.ABNF.OPCODE_PONG:
receive_pong()
return None, None
示例5: _websocket_exc_handler
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def _websocket_exc_handler(method):
def wrapper(self, *args, **kwds):
try:
return method(self, *args, **kwds)
except (TimeoutError, websocket.WebSocketTimeoutException):
self.logger.warning("Websocket timeout")
except (ConnectionError, websocket.WebSocketException) as e:
# If we lose the connection we must reinitialize our state
self.logger.error(str(e))
self._reset_state()
except Exception as e:
self.logger.exception("Websocket callback unhandled exception")
self._reset_state()
return wrapper
示例6: _reset_state
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def _reset_state(self):
self._media_state = None
if self._websocket_fd is not None:
self._pomp_loop_thread.remove_fd_from_loop(self._websocket_fd)
self._websocket_fd = None
if self._websocket is not None:
try:
self._websocket.close()
except websocket.WebSocketException:
pass
finally:
self._websocket = None
示例7: testReadHeader
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def testReadHeader(self):
status, header, status_message = read_headers(HeaderSockMock("data/header01.txt"))
self.assertEqual(status, 101)
self.assertEqual(header["connection"], "Upgrade")
HeaderSockMock("data/header02.txt")
self.assertRaises(ws.WebSocketException, read_headers, HeaderSockMock("data/header02.txt"))
示例8: testRecvContFragmentation
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def testRecvContFragmentation(self):
sock = ws.WebSocket()
s = sock.sock = SockMock()
# OPCODE=CONT, FIN=1, MSG="the soul of wit"
s.add_packet(six.b("\x80\x8fabcd\x15\n\x06D\x12\r\x16\x08A\r\x05D\x16\x0b\x17"))
self.assertRaises(ws.WebSocketException, sock.recv)
示例9: run_forever
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def run_forever(self, *args, **kwargs):
"""
This method is used to run the websocket app continuously.
It will execute callbacks as defined and try to stay connected with the provided
APIs
"""
cnt = 0
while not self.run_event.is_set():
cnt += 1
self.url = next(self.urls)
log.debug("Trying to connect to node %s" % self.url)
try:
# websocket.enableTrace(True)
self.ws = websocket.WebSocketApp(
self.url,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close,
on_open=self.on_open,
)
self.ws.run_forever()
except websocket.WebSocketException:
if self.num_retries >= 0 and cnt > self.num_retries:
raise NumRetriesReached()
sleeptime = (cnt - 1) * 2 if cnt < 10 else 10
if sleeptime:
log.warning(
"Lost connection to node during wsconnect(): %s (%d/%d) "
% (self.url, cnt, self.num_retries)
+ "Retrying in %d seconds" % sleeptime
)
time.sleep(sleeptime)
except KeyboardInterrupt:
self.ws.keep_running = False
return
except Exception as e:
log.critical("{}\n\n{}".format(str(e), traceback.format_exc()))
示例10: queue_poll
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def queue_poll(self, sleep_t=0.5):
"""Put new messages on the queue as they arrive. Blocking in a thread.
Value of sleep is low to improve responsiveness.
"""
connection_alive = True
while self.running:
if self.ws:
def logger_and_close(msg):
self.log.error('Websocket exception', exc_info=True)
if not self.running:
# Tear down has been invoked
# Prepare to exit the program
connection_alive = False # noqa: F841
else:
if not self.number_try_connection:
# Stop everything.
self.teardown()
self._display_ws_warning()
with catch(websocket.WebSocketException, logger_and_close):
result = self.ws.recv()
self.queue.put(result)
if connection_alive:
time.sleep(sleep_t)
示例11: send
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def send(self, msg):
"""Send something to the ensime server."""
def reconnect(e):
self.log.error('send error, reconnecting...', exc_info=True)
self.connect_ensime_server()
if self.ws:
self.ws.send(msg + "\n")
self.log.debug('send: in')
if self.running and self.ws:
with catch(websocket.WebSocketException, reconnect):
self.log.debug('send: sending JSON on WebSocket')
self.ws.send(msg + "\n")
示例12: connect_ensime_server
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def connect_ensime_server(self):
"""Start initial connection with the server."""
self.log.debug('connect_ensime_server: in')
server_v2 = isinstance(self, EnsimeClientV2)
def disable_completely(e):
if e:
self.log.error('connection error: %s', e, exc_info=True)
self.shutdown_server()
self._display_ws_warning()
if self.running and self.number_try_connection:
self.number_try_connection -= 1
if not self.ensime_server:
port = self.ensime.http_port()
uri = "websocket" if server_v2 else "jerky"
self.ensime_server = gconfig["ensime_server"].format(port, uri)
with catch(websocket.WebSocketException, disable_completely):
# Use the default timeout (no timeout).
options = {"subprotocols": ["jerky"]} if server_v2 else {}
options['enable_multithread'] = True
self.log.debug("About to connect to %s with options %s",
self.ensime_server, options)
self.ws = websocket.create_connection(self.ensime_server, **options)
if self.ws:
self.send_request({"typehint": "ConnectionInfoReq"})
else:
# If it hits this, number_try_connection is 0
disable_completely(None)
示例13: connect
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def connect(self):
try:
self.ws = websocket.create_connection(self.url)
except (websocket.WebSocketException, socket.error) as e:
raise ConnectionError(str(e))
示例14: post
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def post(self, channel, message, tries=0):
try:
self._conn.send(json.dumps({'command': 'post', 'channel': channel, 'message': message}))
resp = json.loads(self._conn.recv())
if resp['status'] == 'error':
raise EventPostingError(resp['code'])
else:
return resp['id']
except WebSocketException:
if tries > 10:
raise
self._connect()
return self.post(channel, message, tries + 1)
示例15: last
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocketException [as 别名]
def last(self, tries=0):
try:
self._conn.send('{"command": "last-msg"}')
resp = json.loads(self._conn.recv())
if resp['status'] == 'error':
raise EventPostingError(resp['code'])
else:
return resp['id']
except WebSocketException:
if tries > 10:
raise
self._connect()
return self.last(tries + 1)