1
0
Эх сурвалжийг харах

feat: `websocket_max_message_size` in `start_server()` and `path_deploy` accepts string

wangweimin 4 жил өмнө
parent
commit
514acbde38

+ 3 - 19
pywebio/input.py

@@ -66,7 +66,7 @@ from collections.abc import Mapping
 
 from .io_ctrl import single_input, input_control, output_register_callback
 from .session import get_current_session, get_current_task_id
-from .utils import Setter, is_html_safe_value
+from .utils import Setter, is_html_safe_value, parse_file_size
 
 logger = logging.getLogger(__name__)
 
@@ -500,22 +500,6 @@ def actions(label='', buttons=None, name=None, help_text=None):
     return single_input(item_spec, valid_func, lambda d: d)
 
 
-def _parse_file_size(size):
-    if isinstance(size, (int, float)):
-        return int(size)
-    assert isinstance(size, str), '`size` must be int/float/str, got %s' % type(size)
-
-    size = size.lower()
-
-    for idx, i in enumerate(['k', 'm', 'g'], 1):
-        if i in size:
-            s = size.replace(i, '')
-            base = 2 ** (idx * 10)
-            return int(float(s) * base)
-
-    return int(size)
-
-
 def file_upload(label='', accept=None, name=None, placeholder='Choose file', multiple=False, max_size=0,
                 max_total_size=0, required=None, help_text=None, **other_html_attrs):
     r"""File uploading
@@ -558,8 +542,8 @@ def file_upload(label='', accept=None, name=None, placeholder='Choose file', mul
     """
     item_spec, valid_func = _parse_args(locals())
     item_spec['type'] = 'file'
-    item_spec['max_size'] = _parse_file_size(max_size)
-    item_spec['max_total_size'] = _parse_file_size(max_total_size)
+    item_spec['max_size'] = parse_file_size(max_size)
+    item_spec['max_total_size'] = parse_file_size(max_total_size)
 
     def read_file(data):  # data: None or [{'filename':, 'dataurl', 'mime_type', 'last_modified'}, ...]
         for d in data:

+ 2 - 2
pywebio/platform/path_deploy.py

@@ -10,7 +10,7 @@ from .tornado import webio_handler, set_ioloop
 from .tornado_http import TornadoHttpContext
 from .utils import cdn_validation, make_applications
 from ..session import register_session_implement, CoroutineBasedSession, ThreadBasedSession
-from ..utils import get_free_port, STATIC_PATH
+from ..utils import get_free_port, STATIC_PATH, parse_file_size
 from functools import partial
 
 
@@ -198,7 +198,7 @@ def path_deploy(base, port=0, host='',
                        cdn=cdn, debug=debug,
                        websocket_max_message_size=websocket_max_message_size,
                        websocket_ping_interval=websocket_ping_interval,
-                       websocket_ping_timeout=websocket_ping_timeout,
+                       websocket_ping_timeout=parse_file_size(websocket_ping_timeout or '10M'),
                        **tornado_app_settings)
 
     cdn_url, abs_base = next(gen)

+ 9 - 3
pywebio/platform/tornado.py

@@ -18,7 +18,8 @@ from .utils import make_applications, render_page, cdn_validation
 from ..session import CoroutineBasedSession, ThreadBasedSession, ScriptModeSession, \
     register_session_implement_for_target, Session
 from ..session.base import get_session_info_from_headers
-from ..utils import get_free_port, wait_host_port, STATIC_PATH, iscoroutinefunction, isgeneratorfunction, check_webio_js
+from ..utils import get_free_port, wait_host_port, STATIC_PATH, iscoroutinefunction, isgeneratorfunction, \
+    check_webio_js, parse_file_size
 
 logger = logging.getLogger(__name__)
 
@@ -248,8 +249,11 @@ def start_server(applications, port=0, host='',
        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.
        If ``check_origin`` is set, the ``allowed_origins`` parameter will be ignored.
     :param bool auto_open_webbrowser: Whether or not auto open web browser when server is started (if the operating system allows it) .
-    :param int websocket_max_message_size: Max bytes of a message which Tornado can accept.
-        Messages larger than the ``websocket_max_message_size`` (default 10MiB) will not be accepted.
+    :param int/str websocket_max_message_size: Max bytes of a message which Tornado can accept.
+        Messages larger than the ``websocket_max_message_size`` (default 10MB) will not be accepted.
+        ``websocket_max_message_size`` can be a integer indicating the number of bytes, or a string ending with `K` / `M` / `G`
+        (representing kilobytes, megabytes, and gigabytes, respectively).
+        E.g: ``500``, ``'40K'``, ``'3M'``
     :param int websocket_ping_interval: If set to a number, all websockets will be pinged every n seconds.
         This can help keep the connection alive through certain proxy servers which close idle connections,
         and it can detect if the websocket has failed without being properly closed.
@@ -259,6 +263,8 @@ def start_server(applications, port=0, host='',
     :param tornado_app_settings: Additional keyword arguments passed to the constructor of ``tornado.web.Application``.
         For details, please refer: https://www.tornadoweb.org/en/stable/web.html#tornado.web.Application.settings
     """
+    if websocket_max_message_size:
+        websocket_max_message_size = parse_file_size(websocket_max_message_size)
     kwargs = locals()
 
     set_ioloop(tornado.ioloop.IOLoop.current())  # to enable bokeh app

+ 21 - 0
pywebio/utils.py

@@ -333,3 +333,24 @@ OR
     pip3 install -U https://code.aliyun.com/wang0618/pywebio/repository/archive.zip
 """.strip()
     raise RuntimeError(error_msg)
+
+
+def parse_file_size(size):
+    """Transform file size to byte
+
+    :param str/int/float size: 1, '30', '20M', '32k', '16G', '15mb'
+    :return int: in byte
+    """
+    if isinstance(size, (int, float)):
+        return int(size)
+    assert isinstance(size, str), '`size` must be int/float/str, got %s' % type(size)
+
+    size = size.lower().replace('b', '')
+
+    for idx, i in enumerate(['k', 'm', 'g', 't', 'p'], 1):
+        if i in size:
+            s = size.replace(i, '')
+            base = 2 ** (idx * 10)
+            return int(float(s) * base)
+
+    return int(size)