我正在使用Python Socket.IO 服务器和客户端以及用于 python 的 websocket 客户端来创建从我的 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