Browse Source

Support Python's ConnectionRefusedError to reject a connection

pull/1515/head
Miguel Grinberg 8 months ago
parent
commit
3b39c64fe3
Failed to extract signature
  1. 2
      docs/server.rst
  2. 3
      src/socketio/async_server.py
  3. 3
      src/socketio/server.py

2
docs/server.rst

@ -246,6 +246,8 @@ a :class:`socketio.exceptions.ConnectionRefusedError` exception can be raised,
and all of its arguments will be sent to the client with the rejection and all of its arguments will be sent to the client with the rejection
message:: message::
from socketio.exceptions import ConnectionRefusedError
@sio.event @sio.event
def connect(sid, environ, auth): def connect(sid, environ, auth):
raise ConnectionRefusedError('authentication failed') raise ConnectionRefusedError('authentication failed')

3
src/socketio/async_server.py

@ -561,6 +561,9 @@ class AsyncServer(base_server.BaseServer):
except exceptions.ConnectionRefusedError as exc: except exceptions.ConnectionRefusedError as exc:
fail_reason = exc.error_args fail_reason = exc.error_args
success = False success = False
except ConnectionRefusedError as exc:
fail_reason = {"message": "Connection refused by server"}
success = False
if success is False: if success is False:
if self.always_connect: if self.always_connect:

3
src/socketio/server.py

@ -543,6 +543,9 @@ class Server(base_server.BaseServer):
except exceptions.ConnectionRefusedError as exc: except exceptions.ConnectionRefusedError as exc:
fail_reason = exc.error_args fail_reason = exc.error_args
success = False success = False
except ConnectionRefusedError as exc:
fail_reason = {"message": "Connection refused by server"}
success = False
if success is False: if success is False:
if self.always_connect: if self.always_connect:

Loading…
Cancel
Save