0

I'm making a simple app that uses socketIO_client. I can't figure out how to "catch" the error when the connection fails.

from socketIO_client import SocketIO, LoggingNamespace
sock = SocketIO("localhost", 3000, LoggingNamespace)

When the server is offline the following is printed in the console:

WARNING:root:localhost:3000/socket.io [waiting for connection] HTTPConnectionPool(host='localhost', port=3000): Max retries exceeded with url: /socket.io/?EIO=3&transport=polling&t=1455989769325-0 (Caused by <class 'ConnectionRefusedError'>: [Errno 111] Connection refused)

But an exception isn't raised that I can catch and I tried creating my own namespace class with on_error, on_disconnect, and on_eventfunctions defined but none of them were executed. This answer didn't work either.

How can I "catch" this error so I can handle it properly? Alternatively, where can I find more detailed documentation? Thanks!

Community
  • 1
  • 1
nico
  • 2,022
  • 4
  • 23
  • 37

1 Answers1

1

The LoggingMixin of socketIO_client very explicitly turns the exception into a string and logs it; you'll have to subclass SocketIO and customise the loop that implements retrying things for a number of seconds. If you simply want to let the exception bubble up the first time it occurs (independent of the time-out):

from socketIO_client import SocketIO, LoggingNamespace import socketIO_client.logs

class MySocketIO(SocketIO):

    def _yield_warning_screen(self, seconds=None):
        yield from socketIO_client.logs._yield_elapsed_time(seconds)

sock = MySocketIO("localhost", 3000, LoggingNamespace)

For more complex behaviour like raising only after timing out, a more complex loop implementation in place of the transparent yield from is needed.

Thomas Lotze
  • 5,153
  • 1
  • 16
  • 16