tornado.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. import asyncio
  2. import fnmatch
  3. import json
  4. import logging
  5. import os
  6. import threading
  7. import time
  8. import webbrowser
  9. from functools import partial
  10. from typing import Dict
  11. from urllib.parse import urlparse
  12. import tornado
  13. import tornado.httpserver
  14. import tornado.ioloop
  15. from tornado.web import StaticFileHandler
  16. from tornado.websocket import WebSocketHandler
  17. from .utils import make_applications, render_page, cdn_validation
  18. from ..session import CoroutineBasedSession, ThreadBasedSession, ScriptModeSession, \
  19. register_session_implement_for_target, Session
  20. from ..session.base import get_session_info_from_headers
  21. from ..utils import get_free_port, wait_host_port, STATIC_PATH, iscoroutinefunction, isgeneratorfunction, \
  22. check_webio_js, parse_file_size, random_str, LRUDict
  23. logger = logging.getLogger(__name__)
  24. _ioloop = None
  25. def set_ioloop(loop):
  26. global _ioloop
  27. _ioloop = loop
  28. def ioloop() -> tornado.ioloop.IOLoop:
  29. """获得运行Tornado server的IOLoop
  30. 本方法当前仅在显示boken app时使用
  31. This method is currently only used when displaying boken app"""
  32. global _ioloop
  33. return _ioloop
  34. def _check_origin(origin, allowed_origins, handler: WebSocketHandler):
  35. if _is_same_site(origin, handler):
  36. return True
  37. return any(
  38. fnmatch.fnmatch(origin, patten)
  39. for patten in allowed_origins
  40. )
  41. def _is_same_site(origin, handler: WebSocketHandler):
  42. parsed_origin = urlparse(origin)
  43. origin = parsed_origin.netloc
  44. origin = origin.lower()
  45. host = handler.request.headers.get("Host")
  46. # Check to see that origin matches host directly, including ports
  47. return origin == host
  48. def _webio_handler(applications=None, cdn=True, reconnect_timeout=0, check_origin_func=_is_same_site):
  49. """
  50. :param dict applications: dict of `name -> task function`
  51. :param bool/str cdn: Whether to load front-end static resources from CDN
  52. :param callable check_origin_func: check_origin_func(origin, handler) -> bool
  53. :return: Tornado RequestHandler class
  54. """
  55. check_webio_js()
  56. if applications is None:
  57. applications = dict(index=lambda: None) # mock PyWebIO app
  58. class WSHandler(WebSocketHandler):
  59. def __init__(self, *args, **kwargs):
  60. super().__init__(*args, **kwargs)
  61. self._close_from_session = False
  62. self.session_id = None
  63. self.session = None # type: Session
  64. if reconnect_timeout and not type(self)._started_clean_task:
  65. type(self)._started_clean_task = True
  66. tornado.ioloop.IOLoop.current().call_later(reconnect_timeout // 2, type(self).clean_expired_sessions)
  67. logger.debug("Started session clean task")
  68. def get_app(self):
  69. app_name = self.get_query_argument('app', 'index')
  70. app = applications.get(app_name) or applications['index']
  71. return app
  72. async def get(self, *args, **kwargs) -> None:
  73. # It's a simple http GET request
  74. if self.request.headers.get("Upgrade", "").lower() != "websocket":
  75. # Backward compatible
  76. # Frontend detect whether the backend is http server
  77. if self.get_query_argument('test', ''):
  78. return self.write('')
  79. app = self.get_app()
  80. html = render_page(app, protocol='ws', cdn=cdn)
  81. return self.write(html)
  82. else:
  83. await super().get()
  84. def check_origin(self, origin):
  85. return check_origin_func(origin=origin, handler=self)
  86. def get_compression_options(self):
  87. # Non-None enables compression with default options.
  88. return {}
  89. @classmethod
  90. def clean_expired_sessions(cls):
  91. tornado.ioloop.IOLoop.current().call_later(reconnect_timeout // 2, cls.clean_expired_sessions)
  92. while cls._session_expire:
  93. session_id, expire_ts = cls._session_expire.popitem(last=False) # 弹出最早过期的session
  94. if time.time() < expire_ts:
  95. # this session is not expired
  96. cls._session_expire[session_id] = expire_ts # restore this item
  97. cls._session_expire.move_to_end(session_id, last=False) # move to front
  98. break
  99. # clean this session
  100. logger.debug("session %s expired" % session_id)
  101. cls._connections.pop(session_id, None)
  102. session = cls._webio_sessions.pop(session_id, None)
  103. if session:
  104. session.close(nonblock=True)
  105. @classmethod
  106. def send_msg_to_client(cls, _, session_id=None):
  107. conn = cls._connections.get(session_id)
  108. session = cls._webio_sessions[session_id]
  109. if not conn or not conn.ws_connection:
  110. return
  111. for msg in session.get_task_commands():
  112. conn.write_message(json.dumps(msg))
  113. @classmethod
  114. def close_from_session(cls, session_id=None):
  115. cls.send_msg_to_client(None, session_id=session_id)
  116. conn = cls._connections.pop(session_id, None)
  117. cls._webio_sessions.pop(session_id, None)
  118. if conn and conn.ws_connection:
  119. conn._close_from_session = True
  120. conn.close()
  121. _started_clean_task = False
  122. _session_expire = LRUDict() # session_id -> expire timestamp. In increasing order of expire time
  123. _webio_sessions = {} # type: Dict[str, Session] # session_id -> session
  124. _connections = {} # type: Dict[str, WSHandler] # session_id -> WSHandler
  125. def open(self):
  126. logger.debug("WebSocket opened")
  127. cls = type(self)
  128. self.session_id = self.get_query_argument('session', None)
  129. if self.session_id in ('NEW', None): # 初始请求,创建新 Session
  130. session_info = get_session_info_from_headers(self.request.headers)
  131. session_info['user_ip'] = self.request.remote_ip
  132. session_info['request'] = self.request
  133. session_info['backend'] = 'tornado'
  134. session_info['protocol'] = 'websocket'
  135. application = self.get_app()
  136. self.session_id = random_str(24)
  137. cls._connections[self.session_id] = self
  138. if iscoroutinefunction(application) or isgeneratorfunction(application):
  139. self.session = CoroutineBasedSession(
  140. application, session_info=session_info,
  141. on_task_command=partial(self.send_msg_to_client, session_id=self.session_id),
  142. on_session_close=partial(self.close_from_session, session_id=self.session_id))
  143. else:
  144. self.session = ThreadBasedSession(
  145. application, session_info=session_info,
  146. on_task_command=partial(self.send_msg_to_client, session_id=self.session_id),
  147. on_session_close=partial(self.close_from_session, session_id=self.session_id),
  148. loop=asyncio.get_event_loop())
  149. cls._webio_sessions[self.session_id] = self.session
  150. if reconnect_timeout:
  151. self.write_message(json.dumps(dict(command='set_session_id', spec=self.session_id)))
  152. elif self.session_id not in cls._webio_sessions: # WebIOSession deleted
  153. self.write_message(json.dumps(dict(command='close_session')))
  154. else:
  155. self.session = cls._webio_sessions[self.session_id]
  156. cls._session_expire.pop(self.session_id, None)
  157. cls._connections[self.session_id] = self
  158. cls.send_msg_to_client(self.session, self.session_id)
  159. logger.debug('session id: %s' % self.session_id)
  160. def on_message(self, message):
  161. data = json.loads(message)
  162. if data is not None:
  163. self.session.send_client_event(data)
  164. def on_close(self):
  165. cls = type(self)
  166. cls._connections.pop(self.session_id, None)
  167. if not reconnect_timeout and not self._close_from_session:
  168. self.session.close(nonblock=True)
  169. elif reconnect_timeout:
  170. if self._close_from_session:
  171. cls._webio_sessions.pop(self.session_id, None)
  172. elif self.session:
  173. cls._session_expire[self.session_id] = time.time() + reconnect_timeout
  174. logger.debug("WebSocket closed")
  175. return WSHandler
  176. def webio_handler(applications, cdn=True, reconnect_timeout=0, allowed_origins=None, check_origin=None):
  177. """Get the ``RequestHandler`` class for running PyWebIO applications in Tornado.
  178. The ``RequestHandler`` communicates with the browser by WebSocket protocol.
  179. The arguments of ``webio_handler()`` have the same meaning as for :func:`pywebio.platform.tornado.start_server`
  180. """
  181. applications = make_applications(applications)
  182. for target in applications.values():
  183. register_session_implement_for_target(target)
  184. cdn = cdn_validation(cdn, 'error') # if CDN is not available, raise error
  185. if check_origin is None:
  186. check_origin_func = partial(_check_origin, allowed_origins=allowed_origins or [])
  187. else:
  188. check_origin_func = lambda origin, handler: _is_same_site(origin, handler) or check_origin(origin)
  189. return _webio_handler(applications=applications, cdn=cdn, check_origin_func=check_origin_func,
  190. reconnect_timeout=reconnect_timeout)
  191. async def open_webbrowser_on_server_started(host, port):
  192. url = 'http://%s:%s' % (host, port)
  193. is_open = await wait_host_port(host, port, duration=20)
  194. if is_open:
  195. logger.info('Try open %s in web browser' % url)
  196. webbrowser.open(url)
  197. else:
  198. logger.error('Open %s failed.' % url)
  199. def _setup_server(webio_handler, port=0, host='', static_dir=None, **tornado_app_settings):
  200. if port == 0:
  201. port = get_free_port()
  202. handlers = [(r"/", webio_handler)]
  203. if static_dir is not None:
  204. handlers.append((r"/static/(.*)", StaticFileHandler, {"path": static_dir}))
  205. handlers.append((r"/(.*)", StaticFileHandler, {"path": STATIC_PATH, 'default_filename': 'index.html'}))
  206. app = tornado.web.Application(handlers=handlers, **tornado_app_settings)
  207. server = app.listen(port, address=host)
  208. return server, port
  209. def start_server(applications, port=0, host='',
  210. debug=False, cdn=True, static_dir=None,
  211. reconnect_timeout=0,
  212. allowed_origins=None, check_origin=None,
  213. auto_open_webbrowser=False,
  214. websocket_max_message_size=None,
  215. websocket_ping_interval=None,
  216. websocket_ping_timeout=None,
  217. **tornado_app_settings):
  218. """Start a Tornado server to provide the PyWebIO application as a web service.
  219. The Tornado server communicates with the browser by WebSocket protocol.
  220. Tornado is the default backend server for PyWebIO applications,
  221. and ``start_server`` can be imported directly using ``from pywebio import start_server``.
  222. :param list/dict/callable applications: PyWebIO application.
  223. Can be a task function, a list of functions, or a dictionary.
  224. When it is a dictionary, whose key is task name and value is task function.
  225. When it is a list, using function name as task name.
  226. You can select the task to run through the ``app`` URL parameter (for example, visit ``http://host:port/?app=foo`` to run the ``foo`` task),
  227. By default, the ``index`` task function is used. When the ``index`` task does not exist, PyWebIO will provide a default index home page.
  228. See also :ref:`Server mode <server_and_script_mode>`
  229. When the task function is a coroutine function, use :ref:`Coroutine-based session <coroutine_based_session>` implementation,
  230. otherwise, use thread-based session implementation.
  231. :param int port: The port the server listens on.
  232. When set to ``0``, the server will automatically select a available port.
  233. :param str host: The host the server listens on. ``host`` may be either an IP address or hostname. If it’s a hostname, the server will listen on all IP addresses associated with the name. ``host`` may be an empty string or None to listen on all available interfaces.
  234. :param bool debug: Tornado Server's debug mode. If enabled, the server will automatically reload for code changes.
  235. See `tornado doc <https://www.tornadoweb.org/en/stable/guide/running.html#debug-mode>`_ for more detail.
  236. :param bool/str cdn: Whether to load front-end static resources from CDN, the default is ``True``.
  237. Can also use a string to directly set the url of PyWebIO static resources.
  238. :param str static_dir: The directory to store the application static files.
  239. The files in this directory can be accessed via ``http://<host>:<port>/static/files``.
  240. For example, if there is a ``A/B.jpg`` file in ``http_static_dir`` path,
  241. it can be accessed via ``http://<host>:<port>/static/A/B.jpg``.
  242. :param int reconnect_timeout: The client can reconnect to server within ``reconnect_timeout`` seconds after an unexpected disconnection.
  243. If set to 0 (default), once the client disconnects, the server session will be closed.
  244. :param list allowed_origins: The allowed request source list. (The current server host is always allowed)
  245. The source contains the protocol, domain name, and port part.
  246. Can use Unix shell-style wildcards:
  247. - ``*`` matches everything
  248. - ``?`` matches any single character
  249. - ``[seq]`` matches any character in *seq*
  250. - ``[!seq]`` matches any character not in *seq*
  251. Such as: ``https://*.example.com`` 、 ``*://*.example.com``
  252. For detail, see `Python Doc <https://docs.python.org/zh-tw/3/library/fnmatch.html>`_
  253. :param callable check_origin: The validation function for request source.
  254. It receives the source string (which contains protocol, host, and port parts) as parameter and return ``True/False`` to indicate that the server accepts/rejects the request.
  255. If ``check_origin`` is set, the ``allowed_origins`` parameter will be ignored.
  256. :param bool auto_open_webbrowser: Whether or not auto open web browser when server is started (if the operating system allows it) .
  257. :param int/str websocket_max_message_size: Max bytes of a message which Tornado can accept.
  258. Messages larger than the ``websocket_max_message_size`` (default 10MB) will not be accepted.
  259. ``websocket_max_message_size`` can be a integer indicating the number of bytes, or a string ending with `K` / `M` / `G`
  260. (representing kilobytes, megabytes, and gigabytes, respectively).
  261. E.g: ``500``, ``'40K'``, ``'3M'``
  262. :param int websocket_ping_interval: If set to a number, all websockets will be pinged every n seconds.
  263. This can help keep the connection alive through certain proxy servers which close idle connections,
  264. and it can detect if the websocket has failed without being properly closed.
  265. :param int websocket_ping_timeout: If the ping interval is set, and the server doesn’t receive a ‘pong’
  266. in this many seconds, it will close the websocket. The default is three times the ping interval,
  267. with a minimum of 30 seconds. Ignored if ``websocket_ping_interval`` is not set.
  268. :param tornado_app_settings: Additional keyword arguments passed to the constructor of ``tornado.web.Application``.
  269. For details, please refer: https://www.tornadoweb.org/en/stable/web.html#tornado.web.Application.settings
  270. """
  271. if websocket_max_message_size:
  272. websocket_max_message_size = parse_file_size(websocket_max_message_size)
  273. kwargs = locals()
  274. set_ioloop(tornado.ioloop.IOLoop.current()) # to enable bokeh app
  275. app_options = ['debug', 'websocket_max_message_size', 'websocket_ping_interval', 'websocket_ping_timeout']
  276. for opt in app_options:
  277. if kwargs[opt] is not None:
  278. tornado_app_settings[opt] = kwargs[opt]
  279. cdn = cdn_validation(cdn, 'warn') # if CDN is not available, warn user and disable CDN
  280. handler = webio_handler(applications, cdn, allowed_origins=allowed_origins, check_origin=check_origin,
  281. reconnect_timeout=reconnect_timeout)
  282. _, port = _setup_server(webio_handler=handler, port=port, host=host, static_dir=static_dir, **tornado_app_settings)
  283. print('Listen on %s:%s' % (host or '0.0.0.0', port))
  284. if auto_open_webbrowser:
  285. tornado.ioloop.IOLoop.current().spawn_callback(open_webbrowser_on_server_started, host or 'localhost', port)
  286. tornado.ioloop.IOLoop.current().start()
  287. def start_server_in_current_thread_session():
  288. """启动 script mode 的server,监听可用端口,并自动打开浏览器
  289. Start the server for script mode, and automatically open the browser when the server port is available.
  290. PYWEBIO_SCRIPT_MODE_PORT环境变量可以设置监听端口,并关闭自动打开浏览器,用于测试
  291. The PYWEBIO_SCRIPT_MODE_PORT environment variable can set the listening port, just used in testing.
  292. """
  293. websocket_conn_opened = threading.Event()
  294. thread = threading.current_thread()
  295. class SingleSessionWSHandler(_webio_handler(cdn=False)):
  296. session = None
  297. instance = None
  298. def open(self):
  299. self.main_session = False
  300. cls = type(self)
  301. if SingleSessionWSHandler.session is None:
  302. self.main_session = True
  303. SingleSessionWSHandler.instance = self
  304. self.session_id = 'main'
  305. cls._connections[self.session_id] = self
  306. session_info = get_session_info_from_headers(self.request.headers)
  307. session_info['user_ip'] = self.request.remote_ip
  308. session_info['request'] = self.request
  309. session_info['backend'] = 'tornado'
  310. session_info['protocol'] = 'websocket'
  311. self.session = SingleSessionWSHandler.session = ScriptModeSession(
  312. thread, session_info=session_info,
  313. on_task_command=partial(self.send_msg_to_client, session_id=self.session_id),
  314. loop=asyncio.get_event_loop())
  315. websocket_conn_opened.set()
  316. cls._webio_sessions[self.session_id] = self.session
  317. else:
  318. self.close()
  319. def on_close(self):
  320. if SingleSessionWSHandler.session is not None and self.main_session:
  321. self.session.close()
  322. logger.debug('ScriptModeSession closed')
  323. async def wait_to_stop_loop(server):
  324. """当只剩当前线程和Daemon线程运行时,关闭Server
  325. When only the current thread and Daemon thread are running, close the Server"""
  326. # 包括当前线程在内的非Daemon线程数
  327. # The number of non-Daemon threads(including the current thread)
  328. alive_none_daemonic_thread_cnt = None
  329. while alive_none_daemonic_thread_cnt != 1:
  330. alive_none_daemonic_thread_cnt = sum(
  331. 1 for t in threading.enumerate() if t.is_alive() and not t.isDaemon()
  332. )
  333. await asyncio.sleep(1)
  334. # 关闭Websocket连接
  335. # Close the Websocket connection
  336. if SingleSessionWSHandler.instance:
  337. SingleSessionWSHandler.instance.close()
  338. server.stop()
  339. logger.debug('Closing tornado ioloop...')
  340. tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task() and not t.done()]
  341. for task in tasks: task.cancel()
  342. # 必须需要 await asyncio.sleep ,否则上方 task.cancel() 调用无法调度生效
  343. # This line must be required, otherwise the `task.cancel()` call cannot be scheduled to take effect
  344. await asyncio.sleep(0)
  345. tornado.ioloop.IOLoop.current().stop()
  346. def server_thread():
  347. from tornado.log import access_log, app_log, gen_log
  348. access_log.setLevel(logging.ERROR)
  349. app_log.setLevel(logging.ERROR)
  350. gen_log.setLevel(logging.ERROR)
  351. loop = asyncio.new_event_loop()
  352. asyncio.set_event_loop(loop)
  353. set_ioloop(tornado.ioloop.IOLoop.current()) # to enable bokeh app
  354. port = 0
  355. if os.environ.get("PYWEBIO_SCRIPT_MODE_PORT"):
  356. port = int(os.environ.get("PYWEBIO_SCRIPT_MODE_PORT"))
  357. server, port = _setup_server(webio_handler=SingleSessionWSHandler, port=port, host='localhost')
  358. tornado.ioloop.IOLoop.current().spawn_callback(partial(wait_to_stop_loop, server=server))
  359. if "PYWEBIO_SCRIPT_MODE_PORT" not in os.environ:
  360. tornado.ioloop.IOLoop.current().spawn_callback(open_webbrowser_on_server_started, 'localhost', port)
  361. tornado.ioloop.IOLoop.current().start()
  362. logger.debug('Tornado server exit')
  363. t = threading.Thread(target=server_thread, name='Tornado-server')
  364. t.start()
  365. websocket_conn_opened.wait()