Where can arbitrary exceptions during WebSocket connections in Autobahn be caught in the Python code?

霸气de小男生 提交于 2019-12-11 05:44:10

问题


I have created a Python program that uses Autobahn to make WebSocket connections to a remote host, and receive a data flow over these connections.

From time to time, some different exceptions occur during these connections, most often either an exception immediately when attempting to connect, stating that the initial WebSocket handshake failed (most likely due to an overloaded server), like this:

2017-05-03T20:31:10 dropping connection to peer tcp:1.2.3.4:443 with abort=True: WebSocket opening handshake timeout (peer did not finish the opening handshake in time)

Or a later exception during a successful and ongoing connection, saying that the connection timed out due to lack of pong response to a ping, as follows:

2017-05-04T13:33:40 dropping connection to peer tcp:1.2.3.4:443 with abort=True: WebSocket ping timeout (peer did not respond with pong in time)

2017-05-04T13:33:40 session closed with reason wamp.close.transport_lost [WAMP transport was lost without closing the session before]

2017-05-04T13:33:40 While firing onDisconnect: Traceback (most recent call last):

File "c:\Python36\lib\site-packages\txaio\aio.py", line 450, in done

f._result = x

AttributeError: attribute '_result' of '_asyncio.Future' objects is not writable

As can be seen above, this also triggers some other strange exception in the txaio module in this particular case.

No matter what kind of exception that occurs, I would like to catch them and handle them gracefully, but for some reason the exceptions (none of them) seem to bubble up to the code that initiated these connections (i.e. get caught by my try ... except clause there), which looks like this:

from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.wamp import ApplicationRunner

...

class MyComponent(ApplicationSession):

...

try:
    runner = ApplicationRunner("wss://my.websocket.server.com:443", "realm1")
    runner.run(MyComponent)
except Exception as e:
    print('Unexpected connection error')
    ...

Instead, all these exceptions just hang my program completely after the error messages have been dumped out to the terminal as above, why is this?

So, the question is: How and where in the code can I catch these exceptions that occur during the WebSocket connections in Autobahn, and react/handle them gracefully?

来源:https://stackoverflow.com/questions/43782394/where-can-arbitrary-exceptions-during-websocket-connections-in-autobahn-be-caugh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!