如何捕获WebSocket 502异常

时间:2019-07-07 07:56:01

标签: python python-3.x websocket python-socketio

我正在使用Python Socket.IO server and clientwebsocket client for python创建从Raspberry Pi到AWS上服务器的隧道。

除了由于错误导致服务器断开连接之外,我的隧道还可以正常工作。发生这种情况时,这是我在Raspberry Pi上遇到的异常。

我想捕获thsocket2is异常并在发生异常时执行一些操作,但到目前为止我仍未能捕获。

我尝试了以下操作,但没有成功:

  • 在websocket.WebSocketApp()上添加on_error和on_close方法
  • 使用try-catch环绕websocket.WebSocketApp.run_forever()
  • 使用try-catch环绕socketio.Client.connect()
  • 检查socketio.Client.connect()返回值

我的例外:

Exception in thread Thread-6:
    Traceback (most recent call last):
       File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
      File "/usr/lib/python3.5/threading.py", line 862, in run
        self._target(*self._args, **self._kwargs)
      File "/home/pi/.local/lib/python3.5/site-packages/socketio/client.py", line 439, in _handle_reconnect
        socketio_path=self.socketio_path)
      File "/home/pi/.local/lib/python3.5/site-packages/socketio/client.py", line 208, in connect
    engineio_path=socketio_path)
      File "/home/pi/.local/lib/python3.5/site-packages/engineio/client.py", line 166, in connect
    url, headers, engineio_path)
      File "/home/pi/.local/lib/python3.5/site-packages/engineio/client.py", line 303, in _connect_polling
    if self._connect_websocket(url, headers, engineio_path):
      File "/home/pi/.local/lib/python3.5/site-packages/engineio/client.py", line 333, in _connect_websocket
    websocket_url + self._get_url_timestamp(), header=headers)
      File "/home/pi/.local/lib/python3.5/site-packages/websocket/_core.py", line 511, in create_connection
    websock.connect(url, **options)
      File "/home/pi/.local/lib/python3.5/site-packages/websocket/_core.py", line 223, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)
      File "/home/pi/.local/lib/python3.5/site-packages/websocket/_handshake.py", line 79, in handshake
    status, resp = _get_resp_headers(sock)
      File "/home/pi/.local/lib/python3.5/site-packages/websocket/_handshake.py", line 152, in _get_resp_headers
    raise WebSocketBadStatusException("Handshake status %d %s", status, status_message)
    websocket._exceptions.WebSocketBadStatusException: Handshake status 502 Bad Gateway

0 个答案:

没有答案