tornado.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import asyncio
  2. import fnmatch
  3. import json
  4. import logging
  5. import threading
  6. import webbrowser
  7. from functools import partial
  8. from urllib.parse import urlparse
  9. import tornado
  10. import tornado.httpserver
  11. import tornado.ioloop
  12. import tornado.websocket
  13. from tornado.web import StaticFileHandler
  14. from tornado.websocket import WebSocketHandler
  15. from ..session import CoroutineBasedSession, ThreadBasedSession, ScriptModeSession, \
  16. register_session_implement_for_target, AbstractSession
  17. from ..utils import get_free_port, wait_host_port, STATIC_PATH
  18. logger = logging.getLogger(__name__)
  19. def _check_origin(origin, allowed_origins, handler: WebSocketHandler):
  20. if _is_same_site(origin, handler):
  21. return True
  22. return any(
  23. fnmatch.fnmatch(origin, patten)
  24. for patten in allowed_origins
  25. )
  26. def _is_same_site(origin, handler: WebSocketHandler):
  27. parsed_origin = urlparse(origin)
  28. origin = parsed_origin.netloc
  29. origin = origin.lower()
  30. host = handler.request.headers.get("Host")
  31. # Check to see that origin matches host directly, including ports
  32. return origin == host
  33. def _webio_handler(target, session_cls, check_origin_func=_is_same_site):
  34. """获取用于Tornado进行整合的RequestHandle类
  35. :param target: 任务函数
  36. :param session_cls: 会话实现类
  37. :param callable check_origin_func: check_origin_func(origin, handler) -> bool
  38. :return: Tornado RequestHandle类
  39. """
  40. class WSHandler(WebSocketHandler):
  41. def check_origin(self, origin):
  42. return check_origin_func(origin=origin, handler=self)
  43. def get_compression_options(self):
  44. # Non-None enables compression with default options.
  45. return {}
  46. def send_msg_to_client(self, session: AbstractSession):
  47. for msg in session.get_task_commands():
  48. self.write_message(json.dumps(msg))
  49. def open(self):
  50. logger.debug("WebSocket opened")
  51. self.set_nodelay(True)
  52. self._close_from_session_tag = False # 由session主动关闭连接
  53. if session_cls is CoroutineBasedSession:
  54. self.session = CoroutineBasedSession(target, on_task_command=self.send_msg_to_client,
  55. on_session_close=self.close_from_session)
  56. elif session_cls is ThreadBasedSession:
  57. self.session = ThreadBasedSession(target, on_task_command=self.send_msg_to_client,
  58. on_session_close=self.close_from_session,
  59. loop=asyncio.get_event_loop())
  60. else:
  61. raise RuntimeError("Don't support session type:%s" % session_cls)
  62. def on_message(self, message):
  63. data = json.loads(message)
  64. if data is not None:
  65. self.session.send_client_event(data)
  66. def close_from_session(self):
  67. self._close_from_session_tag = True
  68. self.close()
  69. def on_close(self):
  70. if not self._close_from_session_tag: # 只有在由客户端主动断开连接时,才调用 session.close()
  71. self.session.close()
  72. logger.debug("WebSocket closed")
  73. return WSHandler
  74. def webio_handler(target, allowed_origins=None, check_origin=None):
  75. """获取用于Tornado进行整合的RequestHandle类
  76. :param target: 任务函数。任务函数为协程函数时,使用 :ref:`基于协程的会话实现 <coroutine_based_session>` ;任务函数为普通函数时,使用基于线程的会话实现。
  77. :param list allowed_origins: 除当前域名外,服务器还允许的请求的来源列表。
  78. 来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
  79. - ``*`` 为通配符
  80. - ``?`` 匹配单个字符
  81. - ``[seq]`` 匹配seq内的字符
  82. - ``[!seq]`` 匹配不在seq内的字符
  83. 比如 ``https://*.example.com`` 、 ``*://*.example.com`` 、
  84. :param callable check_origin: 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串,
  85. 返回 ``True/False`` 。若设置了 ``check_origin`` , ``allowed_origins`` 参数将被忽略
  86. :return: Tornado RequestHandle类
  87. """
  88. session_cls = register_session_implement_for_target(target)
  89. if check_origin is None:
  90. check_origin_func = _is_same_site
  91. if allowed_origins:
  92. check_origin_func = partial(_check_origin, allowed_origins=allowed_origins)
  93. else:
  94. check_origin_func = lambda origin, handler: check_origin(origin)
  95. return _webio_handler(target=target, session_cls=session_cls, check_origin_func=check_origin_func)
  96. async def open_webbrowser_on_server_started(host, port):
  97. url = 'http://%s:%s' % (host, port)
  98. is_open = await wait_host_port(host, port, duration=5, delay=0.5)
  99. if is_open:
  100. logger.info('Openning %s' % url)
  101. webbrowser.open(url)
  102. else:
  103. logger.error('Open %s failed.' % url)
  104. def _setup_server(webio_handler, port=0, host='', **tornado_app_settings):
  105. if port == 0:
  106. port = get_free_port()
  107. print('Listen on %s:%s' % (host or '0.0.0.0', port))
  108. handlers = [(r"/io", webio_handler),
  109. (r"/(.*)", StaticFileHandler, {"path": STATIC_PATH, 'default_filename': 'index.html'})]
  110. app = tornado.web.Application(handlers=handlers, **tornado_app_settings)
  111. server = app.listen(port, address=host)
  112. return server, port
  113. def start_server(target, port=0, host='', debug=False,
  114. allowed_origins=None, check_origin=None,
  115. auto_open_webbrowser=False,
  116. websocket_max_message_size=None,
  117. websocket_ping_interval=None,
  118. websocket_ping_timeout=None,
  119. **tornado_app_settings):
  120. """Start a Tornado server to serve `target` function
  121. :param target: 任务函数。任务函数为协程函数时,使用 :ref:`基于协程的会话实现 <coroutine_based_session>` ;任务函数为普通函数时,使用基于线程的会话实现。
  122. :param list allowed_origins: 除当前域名外,服务器还允许的请求的来源列表。
  123. :param port: server bind port. set ``0`` to find a free port number to use
  124. :param host: server bind host. ``host`` may be either an IP address or hostname. If it's a hostname,
  125. the server will listen on all IP addresses associated with the name.
  126. set empty string or to listen on all available interfaces.
  127. :param bool debug: Tornado debug mode
  128. :param list allowed_origins: 除当前域名外,服务器还允许的请求的来源列表。
  129. 来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
  130. - ``*`` 为通配符
  131. - ``?`` 匹配单个字符
  132. - ``[seq]`` 匹配seq内的字符
  133. - ``[!seq]`` 匹配不在seq内的字符
  134. 比如 ``https://*.example.com`` 、 ``*://*.example.com``
  135. :param callable check_origin: 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串,
  136. 返回 ``True/False`` 。若设置了 ``check_origin`` , ``allowed_origins`` 参数将被忽略
  137. :param bool auto_open_webbrowser: Whether or not auto open web browser when server is started (if the operating system allows it) .
  138. :param int websocket_max_message_size: Max bytes of a message which Tornado can accept.
  139. Messages larger than the ``websocket_max_message_size`` (default 10MiB) will not be accepted.
  140. :param int websocket_ping_interval: If set to a number, all websockets will be pinged every n seconds.
  141. This can help keep the connection alive through certain proxy servers which close idle connections,
  142. and it can detect if the websocket has failed without being properly closed.
  143. :param int websocket_ping_timeout: If the ping interval is set, and the server doesn’t receive a ‘pong’
  144. in this many seconds, it will close the websocket. The default is three times the ping interval,
  145. with a minimum of 30 seconds. Ignored if ``websocket_ping_interval`` is not set.
  146. :param tornado_app_settings: Additional keyword arguments passed to the constructor of ``tornado.web.Application``.
  147. ref: https://www.tornadoweb.org/en/stable/web.html#tornado.web.Application.settings
  148. """
  149. kwargs = locals()
  150. app_options = ['debug', 'websocket_max_message_size', 'websocket_ping_interval', 'websocket_ping_timeout']
  151. for opt in app_options:
  152. if kwargs[opt] is not None:
  153. tornado_app_settings[opt] = kwargs[opt]
  154. handler = webio_handler(target, allowed_origins=allowed_origins, check_origin=check_origin)
  155. _, port = _setup_server(webio_handler=handler, port=port, host=host, **tornado_app_settings)
  156. if auto_open_webbrowser:
  157. tornado.ioloop.IOLoop.current().spawn_callback(open_webbrowser_on_server_started, host or 'localhost', port)
  158. tornado.ioloop.IOLoop.current().start()
  159. def start_server_in_current_thread_session():
  160. """启动 script mode 的server"""
  161. websocket_conn_opened = threading.Event()
  162. thread = threading.current_thread()
  163. class SingleSessionWSHandler(_webio_handler(target=None, session_cls=None)):
  164. session = None
  165. def open(self):
  166. self.main_sessin = False
  167. if SingleSessionWSHandler.session is None:
  168. self.main_sessin = True
  169. SingleSessionWSHandler.session = ScriptModeSession(thread,
  170. on_task_command=self.send_msg_to_client,
  171. loop=asyncio.get_event_loop())
  172. websocket_conn_opened.set()
  173. else:
  174. self.close()
  175. def on_close(self):
  176. if SingleSessionWSHandler.session is not None and self.main_sessin:
  177. self.session.close()
  178. logger.debug('ScriptModeSession closed')
  179. async def wait_to_stop_loop():
  180. """当只剩当前线程和Daemon线程运行时,关闭Server"""
  181. alive_none_daemonic_thread_cnt = None
  182. while alive_none_daemonic_thread_cnt != 1:
  183. alive_none_daemonic_thread_cnt = sum(
  184. 1 for t in threading.enumerate() if t.is_alive() and not t.isDaemon()
  185. )
  186. await asyncio.sleep(1)
  187. # 关闭ScriptModeSession。
  188. # 主动关闭ioloop时,SingleSessionWSHandler.on_close 并不会被调用,需要手动关闭session
  189. SingleSessionWSHandler.session.close()
  190. # Current thread is only one none-daemonic-thread, so exit
  191. logger.debug('Closing tornado ioloop...')
  192. tornado.ioloop.IOLoop.current().stop()
  193. def server_thread():
  194. loop = asyncio.new_event_loop()
  195. asyncio.set_event_loop(loop)
  196. server, port = _setup_server(webio_handler=SingleSessionWSHandler, host='localhost')
  197. tornado.ioloop.IOLoop.current().spawn_callback(wait_to_stop_loop)
  198. tornado.ioloop.IOLoop.current().spawn_callback(open_webbrowser_on_server_started, 'localhost', port)
  199. tornado.ioloop.IOLoop.current().start()
  200. logger.debug('Tornado server exit')
  201. t = threading.Thread(target=server_thread, name='Tornado-server')
  202. t.start()
  203. websocket_conn_opened.wait()