0

I'm using Python Socket.IO server and client and websocket client for python to create a tunnel from my Raspberry Pi to my server on AWS.

My tunnel works great, apart from when the server disconnects due to an error. When this happens, this is the exception I get on my Raspberry Pi.

I would like to catch thsocket2is exception and perform some actions when it occurs, but I failed to catch it so far.

I have tried the following, with no success:

  • add on_error and on_close methods on websocket.WebSocketApp()
  • surround websocket.WebSocketApp.run_forever() with a try-catch
  • surround socketio.Client.connect() with a try-catch
  • check for socketio.Client.connect() return value

My exception:

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
Dan Sheinin
  • 59
  • 10
  • What exception are you catching in your try-catch? You should be able to catch `websocket._exceptions.WebSocketBadStatusException` if you wrap the Socket.IO client connect call. – Miguel Grinberg Jul 08 '19 at 11:57
  • Error 502. I tried to catch it with a general try-catch (except Exception) but it failed. – Dan Sheinin Jul 09 '19 at 10:11
  • I suggest that you add the code that you tried to your question then, I don't see why a generic try/except would not catch this error. – Miguel Grinberg Jul 09 '19 at 19:58

0 Answers0