Переглянути джерело

fix: unify `start_server()`'s host、port parameter behavior

wangweimin 4 роки тому
батько
коміт
d27df5a67b
3 змінених файлів з 16 додано та 7 видалено
  1. 6 3
      pywebio/platform/django.py
  2. 9 3
      pywebio/platform/flask.py
  3. 1 1
      pywebio/platform/tornado.py

+ 6 - 3
pywebio/platform/django.py

@@ -6,9 +6,8 @@ from functools import partial
 from django.http import HttpResponse, HttpRequest
 
 from .httpbased import HttpContext, HttpHandler, run_event_loop
-from ..session import register_session_implement_for_target
-from ..utils import STATIC_PATH, iscoroutinefunction, isgeneratorfunction, get_free_port
 from .utils import make_applications
+from ..utils import STATIC_PATH, iscoroutinefunction, isgeneratorfunction, get_free_port
 
 logger = logging.getLogger(__name__)
 
@@ -123,7 +122,8 @@ def start_server(applications, port=8080, host='localhost',
 
     :param list/dict/callable applications: PyWebIO应用. 可以是任务函数或者任务函数的字典或列表。
     :param int port: server bind port. set ``0`` to find a free port number to use
-    :param str host: server bind host. ``host`` may be either an IP address or hostname.  If it's a hostname,
+    :param str host: server bind host. ``host`` may be either an IP address or hostname.
+       set empty string or `None` to listen on all available interfaces.
     :param list allowed_origins: 除当前域名外,服务器还允许的请求的来源列表。
         来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
 
@@ -159,6 +159,9 @@ def start_server(applications, port=8080, host='localhost',
     if port == 0:
         port = get_free_port()
 
+    if not host:
+        host = '0.0.0.0'
+
     django_options.update(dict(
         DEBUG=debug,
         ALLOWED_HOSTS=["*"],  # Disable host header validation

+ 9 - 3
pywebio/platform/flask.py

@@ -11,9 +11,9 @@ import threading
 from flask import Flask, request, send_from_directory, Response
 
 from .httpbased import HttpContext, HttpHandler, run_event_loop
-from ..session import register_session_implement_for_target
-from ..utils import STATIC_PATH, iscoroutinefunction, isgeneratorfunction
 from .utils import make_applications
+from ..utils import STATIC_PATH, iscoroutinefunction, isgeneratorfunction
+from ..utils import get_free_port
 
 logger = logging.getLogger(__name__)
 
@@ -123,7 +123,8 @@ def start_server(applications, port=8080, host='localhost',
 
     :param list/dict/callable applications: PyWebIO应用. 可以是任务函数或者任务函数的字典或列表。
     :param int port: server bind port. set ``0`` to find a free port number to use
-    :param str host: server bind host. ``host`` may be either an IP address or hostname.  If it's a hostname,
+    :param str host: server bind host. ``host`` may be either an IP address or hostname.
+       set empty string or `None` to listen on all available interfaces.
     :param list allowed_origins: 除当前域名外,服务器还允许的请求的来源列表。
         来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
 
@@ -147,6 +148,11 @@ def start_server(applications, port=8080, host='localhost',
     :param flask_options: Additional keyword arguments passed to the constructor of ``flask.Flask.run``.
         ref: https://flask.palletsprojects.com/en/1.1.x/api/?highlight=flask%20run#flask.Flask.run
     """
+    if not host:
+        host = '0.0.0.0'
+
+    if port == 0:
+        port = get_free_port()
 
     app = Flask(__name__)
     app.add_url_rule('/io', 'webio_view', webio_view(

+ 1 - 1
pywebio/platform/tornado.py

@@ -187,7 +187,7 @@ def start_server(applications, port=0, host='', debug=False,
     :param int port: server bind port. set ``0`` to find a free port number to use
     :param str host: server bind host. ``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.
-        set empty string or to listen on all available interfaces.
+        set empty string or `None` to listen on all available interfaces.
     :param bool debug: Tornado debug mode
     :param list allowed_origins: 除当前域名外,服务器还允许的请求的来源列表。
         来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式: