question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can I catch websockets exception?

See original GitHub issue

HI, I try to catch an exception “Connection reset by peer” when server-client is on heartbeat phase; unfortunately, I can not get the exception of websockets object via class ChargePoint. How can I do? Thanks I try to modify the code like this


    async def start(self):
        while True:
            try:
                message = await self._connection.recv()
                LOGGER.info('%s: receive message %s', self.id, message)               
                await self.route_message(message)
            except self._connection.exceptions as e: #fail
                print('websockets.exceptions')
            except IOError as err:
                print(self.id, 'disconnected')
            finally:
                pass

error message like this File "C:\Users\W8-PC\AppData\Local\Programs\Python\Python37\lib\asyncio\selector_events.py", line 801, in _read_ready__data_received data = self._sock.recv(self.max_size) ConnectionResetError: [WinError 10054] and websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
OrangeTuxcommented, Jun 11, 2019

Happy to help! I will open this ticket now. Feel free to open a new one if you have more questions.

0reactions
Buco11-devcommented, Apr 9, 2022

Hey @OrangeTux I know this issue is closed but I was wondering if you could help me with something similar to your answer above to monitor a web3 node WebSocket connection in the background while the rest of the code runs and can auto-reconnect when it disconnects.

Here’s how I connect currently:

from web3 import Web3
node = 'wss://bsc-ws-node.nariox.org:443'
web3 = Web3(Web3.WebsocketProvider(node, websocket_timeout=60))
try:
    if web3.isConnected():
            print("Successfully connected to network")
    except Exception as error:
        print(f"[Connection Error]: {error}")

Thanks in advance!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exceptions - websockets 10.4 documentation
websockets.exceptions defines the following exception hierarchy: ... Raised to abort the handshake on purpose and return a HTTP response. This exception is an ......
Read more >
Handling websockets module exceptions and errors
I'm trying to create a simple WebSocket server using websockets ... I don't really understand how can I catch those exceptions thrown by ......
Read more >
Python websockets.exceptions.ConnectionClosed() Examples
This page shows Python examples of websockets.exceptions. ... Can raise ConnectionClosed data: Data = await self.websocket.recv() # websocket.recv() can ...
Read more >
WebSockets - Handling Errors
WebSockets - Handling Errors, Once a connection has been established between the client and the server, an open event is fired from the...
Read more >
WebSocket: error event - Web APIs | MDN
The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found