path_deploy.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import os.path
  2. from functools import partial
  3. import tornado
  4. from tornado import template
  5. from tornado.web import HTTPError, Finish
  6. from tornado.web import StaticFileHandler
  7. from . import utils
  8. from .httpbased import HttpHandler
  9. from .tornado import webio_handler, set_ioloop
  10. from .tornado_http import TornadoHttpContext
  11. from .utils import cdn_validation, make_applications
  12. from ..session import register_session_implement, CoroutineBasedSession, ThreadBasedSession
  13. from ..utils import get_free_port, STATIC_PATH, parse_file_size
  14. def filename_ok(f):
  15. return not f.startswith(('.', '_'))
  16. def valid_and_norm_path(base, subpath):
  17. """
  18. :param str base: MUST a absolute path
  19. :param str subpath:
  20. :return: Normalize path. None returned if the sub path is not valid
  21. """
  22. subpath = subpath.lstrip('/')
  23. full_path = os.path.normpath(os.path.join(base, subpath))
  24. if not full_path.startswith(base):
  25. return None
  26. parts = subpath.split('/')
  27. for i in parts:
  28. if not filename_ok(i):
  29. return None
  30. return full_path
  31. _cached_modules = {}
  32. def _get_module(path, reload=False):
  33. # Credit: https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path
  34. global _cached_modules
  35. import importlib.util
  36. if not reload and path in _cached_modules:
  37. return _cached_modules[path]
  38. # import_name will be the `__name__` of the imported module
  39. import_name = "__pywebio__"
  40. spec = importlib.util.spec_from_file_location(import_name, path)
  41. module = importlib.util.module_from_spec(spec)
  42. spec.loader.exec_module(module)
  43. _cached_modules[path] = module
  44. return module
  45. _app_list_tpl = template.Template("""
  46. <!DOCTYPE html>
  47. <html lang="">
  48. <head>
  49. <meta charset="UTF-8">
  50. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  51. <title>{{ title }}</title>
  52. <meta name="description" content="PyWebIO applications index">
  53. <style>a{text-decoration:none}</style>
  54. </head>
  55. <body>
  56. <h1>{{ title }}</h1>
  57. <hr>
  58. <pre style="line-height: 1.6em; font-size: 16px;">
  59. {% for f in files %} <a href="{{ f }}">{{ f }}</a>
  60. {% end %}</pre>
  61. <hr>
  62. </body>
  63. </html>
  64. """.strip())
  65. def default_index_page(path, base):
  66. urlpath = path[len(base):] or '/'
  67. title = "Index of %s" % urlpath
  68. dirs = [] if path == base else ['../']
  69. files = []
  70. for f in os.listdir(path):
  71. if not filename_ok(f):
  72. continue
  73. if os.path.isfile(os.path.join(path, f)):
  74. if f.endswith('.py'):
  75. files.append(f[:-3])
  76. else:
  77. dirs.append(f + '/')
  78. return _app_list_tpl.generate(files=dirs + files, title=title)
  79. def get_app_from_path(request_path, base, index, reload=False):
  80. """Get PyWebIO app
  81. :param str request_path: request path
  82. :param str base: dir base path, MUST a absolute path
  83. :param callable index:
  84. :return: ('error', http error code in int) / ('app', pywebio task function) / ('html', Html content in bytes)
  85. """
  86. path = valid_and_norm_path(base, request_path)
  87. if path is None:
  88. return 'error', 403
  89. if os.path.isdir(path):
  90. if not request_path.endswith('/'):
  91. return 'error', 404
  92. if os.path.isfile(os.path.join(path, 'index.py')):
  93. path = os.path.join(path, 'index.py')
  94. elif index:
  95. content = index(path)
  96. return 'html', content
  97. else:
  98. return 'error', 404
  99. else:
  100. path += '.py'
  101. if not os.path.isfile(path):
  102. return 'error', 404
  103. module = _get_module(path, reload=reload)
  104. if hasattr(module, 'main'):
  105. return 'app', make_applications(module.main)
  106. return 'error', 404
  107. def _path_deploy(base, port=0, host='', static_dir=None, cdn=True, max_payload_size=2 ** 20 * 200,
  108. **tornado_app_settings):
  109. if not host:
  110. host = '0.0.0.0'
  111. if port == 0:
  112. port = get_free_port()
  113. tornado_app_settings = {k: v for k, v in tornado_app_settings.items() if v is not None}
  114. abs_base = os.path.normpath(os.path.abspath(base))
  115. cdn = cdn_validation(cdn, 'warn', stacklevel=4) # if CDN is not available, warn user and disable CDN
  116. cdn_url = '/_pywebio_static/' if not cdn else cdn
  117. register_session_implement(CoroutineBasedSession)
  118. register_session_implement(ThreadBasedSession)
  119. RequestHandler = yield cdn_url, abs_base
  120. handlers = []
  121. if static_dir is not None:
  122. handlers.append((r"/static/(.*)", StaticFileHandler, {"path": static_dir}))
  123. if not cdn:
  124. handlers.append((r"/_pywebio_static/(.*)", StaticFileHandler, {"path": STATIC_PATH}))
  125. handlers.append((r"/.*", RequestHandler))
  126. print('Listen on %s:%s' % (host or '0.0.0.0', port))
  127. set_ioloop(tornado.ioloop.IOLoop.current()) # to enable bokeh app
  128. app = tornado.web.Application(handlers=handlers, **tornado_app_settings)
  129. app.listen(port, address=host, max_buffer_size=max_payload_size)
  130. tornado.ioloop.IOLoop.current().start()
  131. def path_deploy(base, port=0, host='',
  132. index=True, static_dir=None,
  133. reconnect_timeout=0,
  134. cdn=True, debug=True,
  135. allowed_origins=None, check_origin=None,
  136. max_payload_size='200M',
  137. **tornado_app_settings):
  138. """Deploy the PyWebIO applications from a directory.
  139. The server communicates with the browser using WebSocket protocol.
  140. :param str base: Base directory to load PyWebIO application.
  141. :param int port: The port the server listens on.
  142. :param str host: The host the server listens on.
  143. :param bool/callable index: Whether to provide a default index page when request a directory, default is ``True``.
  144. ``index`` also accepts a function to custom index page, which receives the requested directory path as parameter
  145. and return HTML content in string.
  146. You can override the index page by add a `index.py` PyWebIO app file to the directory.
  147. :param str static_dir: Directory to store the application static files.
  148. The files in this directory can be accessed via ``http://<host>:<port>/static/files``.
  149. For example, if there is a ``A/B.jpg`` file in ``http_static_dir`` path,
  150. it can be accessed via ``http://<host>:<port>/static/A/B.jpg``.
  151. :param int reconnect_timeout: The client can reconnect to server within ``reconnect_timeout`` seconds after an unexpected disconnection.
  152. If set to 0 (default), once the client disconnects, the server session will be closed.
  153. The rest arguments of ``path_deploy()`` have the same meaning as for :func:`pywebio.platform.tornado.start_server`
  154. """
  155. utils.MAX_PAYLOAD_SIZE = max_payload_size = parse_file_size(max_payload_size)
  156. tornado_app_settings.setdefault('websocket_max_message_size', max_payload_size) # Backward compatible
  157. tornado_app_settings['websocket_max_message_size'] = parse_file_size(tornado_app_settings['websocket_max_message_size'])
  158. gen = _path_deploy(base, port=port, host=host,
  159. static_dir=static_dir,
  160. cdn=cdn, debug=debug,
  161. max_payload_size=max_payload_size,
  162. **tornado_app_settings)
  163. cdn_url, abs_base = next(gen)
  164. index_func = {True: partial(default_index_page, base=abs_base), False: lambda p: '403 Forbidden'}.get(index, index)
  165. Handler = webio_handler(lambda: None, cdn_url, allowed_origins=allowed_origins,
  166. check_origin=check_origin, reconnect_timeout=reconnect_timeout)
  167. class WSHandler(Handler):
  168. def get_app(self):
  169. reload = self.get_query_argument('reload', None) is not None
  170. type, res = get_app_from_path(self.request.path, abs_base, index=index_func, reload=reload)
  171. if type == 'error':
  172. raise HTTPError(status_code=res)
  173. elif type == 'html':
  174. raise Finish(res)
  175. app_name = self.get_query_argument('app', 'index')
  176. app = res.get(app_name) or res['index']
  177. return app
  178. gen.send(WSHandler)
  179. gen.close()
  180. def path_deploy_http(base, port=0, host='',
  181. index=True, static_dir=None,
  182. cdn=True, debug=True,
  183. allowed_origins=None, check_origin=None,
  184. session_expire_seconds=None,
  185. session_cleanup_interval=None,
  186. max_payload_size='200M',
  187. **tornado_app_settings):
  188. """Deploy the PyWebIO applications from a directory.
  189. The server communicates with the browser using HTTP protocol.
  190. The ``base``, ``port``, ``host``, ``index``, ``static_dir`` arguments of ``path_deploy_http()``
  191. have the same meaning as for :func:`pywebio.platform.path_deploy`
  192. The rest arguments of ``path_deploy_http()`` have the same meaning as for :func:`pywebio.platform.tornado_http.start_server`
  193. """
  194. utils.MAX_PAYLOAD_SIZE = max_payload_size = parse_file_size(max_payload_size)
  195. gen = _path_deploy(base, port=port, host=host,
  196. static_dir=static_dir,
  197. cdn=cdn, debug=debug,
  198. max_payload_size=max_payload_size,
  199. **tornado_app_settings)
  200. cdn_url, abs_base = next(gen)
  201. index_func = {True: partial(default_index_page, base=abs_base), False: lambda p: '403 Forbidden'}.get(index, index)
  202. def get_app(context: TornadoHttpContext):
  203. reload = context.request_url_parameter('reload', None) is not None
  204. type, res = get_app_from_path(context.get_path(), abs_base, index=index_func, reload=reload)
  205. if type == 'error':
  206. raise HTTPError(status_code=res)
  207. elif type == 'html':
  208. raise Finish(res)
  209. app_name = context.request_url_parameter('app', 'index')
  210. return res.get(app_name) or res['index']
  211. handler = HttpHandler(app_loader=get_app, cdn=cdn_url,
  212. session_expire_seconds=session_expire_seconds,
  213. session_cleanup_interval=session_cleanup_interval,
  214. allowed_origins=allowed_origins,
  215. check_origin=check_origin)
  216. class ReqHandler(tornado.web.RequestHandler):
  217. def options(self):
  218. return self.get()
  219. def post(self):
  220. return self.get()
  221. def get(self):
  222. context = TornadoHttpContext(self)
  223. self.write(handler.handle_request(context))
  224. gen.send(ReqHandler)
  225. gen.close()