Websocket Python tornado,在open()中给出了一个错误
我正在用tornado(当前版本为3.1)实现一个web套接字服务器 在Websocket Python tornado,在open()中给出了一个错误,websocket,tornado,Websocket,Tornado,我正在用tornado(当前版本为3.1)实现一个web套接字服务器 在open()函数中,我检查GET参数,然后基于它-我想引发一个错误 大概是这样的: def open(self): token = self.get_argument('token') if ...: ??? # raise an error 如何在open函数中引发错误?我没有找到这样做的方法 谢谢您可以像平常一样引发异常: class EchoWebSocket(websocket.WebSoc
open()
函数中,我检查GET参数,然后基于它-我想引发一个错误
大概是这样的:
def open(self):
token = self.get_argument('token')
if ...:
??? # raise an error
如何在open函数中引发错误?我没有找到这样做的方法
谢谢您可以像平常一样引发异常:
class EchoWebSocket(websocket.WebSocketHandler):
def open(self):
if some_error:
raise Exception("Some error occurred")
当open
中出现未经处理的异常时,Tornado将中止连接。以下是如何计划在tornado source中运行open
:
self._run_callback(self.handler.open, *self.handler.open_args,
**self.handler.open_kwargs)
下面是\u run\u callback
:
def _run_callback(self, callback, *args, **kwargs):
"""Runs the given callback with exception handling.
On error, aborts the websocket connection and returns False.
"""
try:
callback(*args, **kwargs)
except Exception:
app_log.error("Uncaught exception in %s",
self.request.path, exc_info=True)
self._abort()
def _abort(self):
"""Instantly aborts the WebSocket connection by closing the socket"""
self.client_terminated = True
self.server_terminated = True
self.stream.close() # forcibly tear down the connection
self.close() # let the subclass cleanup
如您所见,当发生异常时,它会中止连接