Prechádzať zdrojové kódy

feat: add http-based tornado backend

wangweimin 4 rokov pred
rodič
commit
fce4ffae06

+ 13 - 0
pywebio/platform/__init__.py

@@ -13,9 +13,22 @@ See also: :ref:`Integration with Web Framework <integration_web_framework>` sect
 
 Tornado support
 -----------------
+
+There are two protocols (WebSocket and HTTP) can be used to communicates with the browser:
+
+WebSocket
+^^^^^^^^^^^
+
 .. autofunction:: pywebio.platform.tornado.start_server
 .. autofunction:: pywebio.platform.tornado.webio_handler
 
+HTTP
+^^^^^^^^^^^
+
+.. autofunction:: pywebio.platform.tornado_http.start_server
+.. autofunction:: pywebio.platform.tornado_http.webio_handler
+
+
 Flask support
 -----------------
 

+ 1 - 0
pywebio/platform/httpbased.py

@@ -200,6 +200,7 @@ class HttpHandler:
             session_info['user_ip'] = context.get_client_ip()
             session_info['request'] = context.request_obj()
             session_info['backend'] = context.backend_name
+            session_info['protocol'] = 'http'
 
             app_name = context.request_url_parameter('app', 'index')
             application = self.applications.get(app_name) or self.applications['index']

+ 2 - 0
pywebio/platform/tornado.py

@@ -116,6 +116,7 @@ def _webio_handler(applications=None, cdn=True, check_origin_func=_is_same_site)
             session_info['user_ip'] = self.request.remote_ip
             session_info['request'] = self.request
             session_info['backend'] = 'tornado'
+            session_info['protocol'] = 'websocket'
 
             application = self.get_app()
             if iscoroutinefunction(application) or isgeneratorfunction(application):
@@ -302,6 +303,7 @@ def start_server_in_current_thread_session():
                 session_info['user_ip'] = self.request.remote_ip
                 session_info['request'] = self.request
                 session_info['backend'] = 'tornado'
+                session_info['protocol'] = 'websocket'
                 SingleSessionWSHandler.session = ScriptModeSession(thread, session_info=session_info,
                                                                    on_task_command=self.send_msg_to_client,
                                                                    loop=asyncio.get_event_loop())

+ 165 - 0
pywebio/platform/tornado_http.py

@@ -0,0 +1,165 @@
+import json
+import logging
+
+import tornado.ioloop
+import tornado.web
+
+from .httpbased import HttpContext, HttpHandler
+from .tornado import set_ioloop, _setup_server, open_webbrowser_on_server_started
+from .utils import cdn_validation
+from ..utils import get_free_port
+
+logger = logging.getLogger(__name__)
+
+
+class TornadoHttpContext(HttpContext):
+    backend_name = 'tornado'
+
+    def __init__(self, handler: tornado.web.RequestHandler):
+        self.handler = handler
+        self.response = b''
+
+    def request_obj(self):
+        """返回当前请求对象"""
+        return self.handler.request
+
+    def request_method(self):
+        """返回当前请求的方法,大写"""
+        return self.handler.request.method.upper()
+
+    def request_headers(self):
+        """返回当前请求的header字典"""
+        return self.handler.request.headers
+
+    def request_url_parameter(self, name, default=None):
+        """返回当前请求的URL参数"""
+        return self.handler.get_query_argument(name, default=default)
+
+    def request_json(self):
+        """返回当前请求的json反序列化后的内容,若请求数据不为json格式,返回None"""
+        try:
+            return json.loads(self.handler.request.body.decode('utf8'))
+        except Exception:
+            return None
+
+    def set_header(self, name, value):
+        """为当前响应设置header"""
+        self.handler.set_header(name, value)
+
+    def set_status(self, status: int):
+        """为当前响应设置http status"""
+        self.handler.set_status(status)
+
+    def set_content(self, content, json_type=False):
+        """设置相应的内容
+
+        :param content:
+        :param bool json_type: content是否要序列化成json格式,并将 content-type 设置为application/json
+        """
+        # self.response.content accept str and byte
+        if json_type:
+            self.set_header('content-type', 'application/json')
+            self.response = json.dumps(content)
+        else:
+            self.response = content
+
+    def get_response(self):
+        """获取当前的响应对象,用于在私图函数中返回"""
+        return self.response
+
+    def get_client_ip(self):
+        """获取用户的ip"""
+        return self.handler.request.remote_ip
+
+    def get_path(self):
+        """Get the path patton of the http request uri"""
+        return self.handler.request.path
+
+
+def webio_handler(applications, cdn=True,
+                  session_expire_seconds=None,
+                  session_cleanup_interval=None,
+                  allowed_origins=None, check_origin=None):
+    """Get the ``RequestHandler`` class for running PyWebIO applications in Tornado.
+    The ``RequestHandler``  communicates with the browser by HTTP protocol.
+
+    The arguments of ``webio_handler()`` have the same meaning as for :func:`pywebio.platform.tornado_http.start_server`
+
+    .. versionadded:: 1.2
+    """
+    cdn = cdn_validation(cdn, 'error')  # if CDN is not available, raise error
+
+    handler = HttpHandler(applications=applications, cdn=cdn,
+                          session_expire_seconds=session_expire_seconds,
+                          session_cleanup_interval=session_cleanup_interval,
+                          allowed_origins=allowed_origins, check_origin=check_origin)
+
+    class MainHandler(tornado.web.RequestHandler):
+        def options(self):
+            return self.get()
+
+        def post(self):
+            return self.get()
+
+        def get(self):
+            context = TornadoHttpContext(self)
+            self.write(handler.handle_request(context))
+
+    return MainHandler
+
+
+def start_server(applications, port=8080, host='localhost',
+                 debug=False, cdn=True, static_dir=None,
+                 allowed_origins=None, check_origin=None,
+                 auto_open_webbrowser=False,
+                 session_expire_seconds=None,
+                 session_cleanup_interval=None,
+                 websocket_max_message_size=None,
+                 websocket_ping_interval=None,
+                 websocket_ping_timeout=None,
+                 **tornado_app_settings):
+    """Start a Tornado server to provide the PyWebIO application as a web service.
+
+    The Tornado server communicates with the browser by HTTP protocol.
+
+    :param int session_expire_seconds: Session expiration time, in seconds(default 60s).
+       If no client message is received within ``session_expire_seconds``, the session will be considered expired.
+    :param int session_cleanup_interval: Session cleanup interval, in seconds(default 120s).
+       The server will periodically clean up expired sessions and release the resources occupied by the sessions.
+
+    The rest arguments of ``start_server()`` have the same meaning as for :func:`pywebio.platform.tornado.start_server`
+
+    .. versionadded:: 1.2
+    """
+
+    if port == 0:
+        port = get_free_port()
+
+    if not host:
+        host = '0.0.0.0'
+
+    cdn = cdn_validation(cdn, 'warn')
+
+    kwargs = locals()
+
+    set_ioloop(tornado.ioloop.IOLoop.current())  # to enable bokeh app
+
+    app_options = ['debug', 'websocket_max_message_size', 'websocket_ping_interval', 'websocket_ping_timeout']
+    for opt in app_options:
+        if kwargs[opt] is not None:
+            tornado_app_settings[opt] = kwargs[opt]
+
+    cdn = cdn_validation(cdn, 'warn')  # if CDN is not available, warn user and disable CDN
+
+    handler = webio_handler(applications, cdn,
+                            session_expire_seconds=session_expire_seconds,
+                            session_cleanup_interval=session_cleanup_interval,
+                            allowed_origins=allowed_origins, check_origin=check_origin)
+
+    _, port = _setup_server(webio_handler=handler, port=port, host=host, static_dir=static_dir, **tornado_app_settings)
+
+    print('Listen on %s:%s' % (host or '0.0.0.0', port))
+    if auto_open_webbrowser:
+        tornado.ioloop.IOLoop.current().spawn_callback(open_webbrowser_on_server_started, host or 'localhost', port)
+
+    tornado.ioloop.IOLoop.current().start()

+ 6 - 0
pywebio/session/__init__.py

@@ -115,6 +115,7 @@ r"""
          It may be empty, but it is guaranteed to have a value when the user's page address is not under the server host. (that is, the host, port part are inconsistent with ``server_host``).
        * ``user_ip`` (str): User's ip address.
        * ``backend`` (str): The current PyWebIO backend server implementation. The possible values are ``'tornado'``, ``'flask'``, ``'django'`` , ``'aiohttp'``.
+       * ``protocol`` (str): The communication protocol between PyWebIO server and browser. The possible values are ``'websocket'``, ``'http'``
        * ``request`` (object): The request object when creating the current session. Depending on the backend server, the type of ``request`` can be:
 
             * When using Tornado, ``request`` is instance of
@@ -125,6 +126,10 @@ r"""
 
     The ``user_agent`` attribute of the session information object is parsed by the user-agents library. See https://github.com/selwin/python-user-agents#usage
 
+    .. versionchanged:: 1.2
+
+       Added the ``protocol`` attribute.
+
     Example:
 
     .. exportable-codeblock::
@@ -504,6 +509,7 @@ class _SessionInfoType:
     origin = ''  # e.g.: http://localhost:8080
     user_ip = ''
     backend = ''  # one of ['tornado', 'flask', 'django', 'aiohttp']
+    protocol = ''  # one of ['websocket', 'http']
     request = None
 
 

+ 56 - 0
test/15.tornado_http_multiple_session.py

@@ -0,0 +1,56 @@
+import subprocess
+import time
+
+from selenium.webdriver import Chrome
+
+import pywebio
+import template
+import util
+from pywebio.input import *
+from pywebio.utils import to_coroutine, run_as_function
+
+
+def target():
+    template.basic_output()
+    template.background_output()
+
+    run_as_function(template.basic_input())
+    actions(buttons=['Continue'])
+    template.background_input()
+
+
+async def async_target():
+    template.basic_output()
+    await template.coro_background_output()
+
+    await to_coroutine(template.basic_input())
+    await actions(buttons=['Continue'])
+    await template.coro_background_input()
+
+
+def test(server_proc: subprocess.Popen, browser: Chrome):
+    template.test_output(browser)
+    time.sleep(1)
+    template.test_input(browser)
+    time.sleep(1)
+    template.save_output(browser, '15.tornado_http_multiple_session_p1.html')
+
+    browser.get('http://localhost:8080/?_pywebio_debug=1&_pywebio_http_pull_interval=400&app=p2')
+    template.test_output(browser)
+    time.sleep(1)
+    template.test_input(browser)
+
+    time.sleep(1)
+    template.save_output(browser, '15.tornado_http_multiple_session_p2.html')
+
+
+def start_test_server():
+    pywebio.enable_debug()
+    from pywebio.platform.tornado_http import start_server
+
+    start_server({'p1': target, 'p2': async_target}, port=8080, host='127.0.0.1')
+
+
+if __name__ == '__main__':
+    util.run_test(start_test_server, test,
+                  address='http://localhost:8080/?_pywebio_debug=1&_pywebio_http_pull_interval=400&app=p1')