Skip to content

Commit f6fdde9

Browse files
committed
refine app listen address message
1 parent 3bc7d36 commit f6fdde9

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

pywebio/platform/aiohttp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from .remote_access import start_remote_access_service
1313
from .tornado import open_webbrowser_on_server_started
14-
from .utils import make_applications, render_page, cdn_validation, deserialize_binary_event
14+
from .utils import make_applications, render_page, cdn_validation, deserialize_binary_event, print_listen_address
1515
from ..session import CoroutineBasedSession, ThreadBasedSession, register_session_implement_for_target, Session
1616
from ..session.base import get_session_info_from_headers
1717
from ..utils import get_free_port, STATIC_PATH, iscoroutinefunction, isgeneratorfunction
@@ -205,7 +205,7 @@ def start_server(applications, port=0, host='', debug=False,
205205
if debug:
206206
logging.getLogger("asyncio").setLevel(logging.DEBUG)
207207

208-
print('Listen on %s:%s' % (host, port))
208+
print_listen_address(host, port)
209209

210210
if remote_access:
211211
start_remote_access_service(local_port=port)

pywebio/platform/path_deploy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .httpbased import HttpHandler
1313
from .tornado import webio_handler, set_ioloop
1414
from .tornado_http import TornadoHttpContext
15-
from .utils import cdn_validation, make_applications
15+
from .utils import cdn_validation, make_applications, print_listen_address
1616
from ..session import register_session_implement, CoroutineBasedSession, ThreadBasedSession, Session
1717
from ..utils import get_free_port, STATIC_PATH, parse_file_size
1818

@@ -209,7 +209,7 @@ def _path_deploy(base, port=0, host='', static_dir=None, cdn=True, max_payload_s
209209
handlers.append((r"/_pywebio_static/(.*)", StaticFileHandler, {"path": STATIC_PATH}))
210210
handlers.append((r"/.*", RequestHandler))
211211

212-
print('Listen on %s:%s' % (host or '0.0.0.0', port))
212+
print_listen_address(host, port)
213213

214214
set_ioloop(tornado.ioloop.IOLoop.current()) # to enable bokeh app
215215
app = tornado.web.Application(handlers=handlers, **tornado_app_settings)

pywebio/platform/tornado.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from . import utils
2020
from .remote_access import start_remote_access_service
21-
from .utils import make_applications, render_page, cdn_validation, deserialize_binary_event
21+
from .utils import make_applications, render_page, cdn_validation, deserialize_binary_event, print_listen_address
2222
from ..session import CoroutineBasedSession, ThreadBasedSession, ScriptModeSession, \
2323
register_session_implement_for_target, Session
2424
from ..session.base import get_session_info_from_headers
@@ -361,7 +361,7 @@ def start_server(applications, port=0, host='',
361361
_, port = _setup_server(webio_handler=handler, port=port, host=host, static_dir=static_dir,
362362
max_buffer_size=max_payload_size, **tornado_app_settings)
363363

364-
print('Listen on %s:%s' % (host or '0.0.0.0', port))
364+
print_listen_address(host, port)
365365

366366
if auto_open_webbrowser:
367367
tornado.ioloop.IOLoop.current().spawn_callback(open_webbrowser_on_server_started, host or 'localhost', port)

pywebio/platform/tornado_http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ..session import Session
1010
from .httpbased import HttpContext, HttpHandler
1111
from .tornado import set_ioloop, _setup_server, open_webbrowser_on_server_started
12-
from .utils import cdn_validation
12+
from .utils import cdn_validation, print_listen_address
1313
from ..utils import parse_file_size
1414

1515
logger = logging.getLogger(__name__)
@@ -157,7 +157,7 @@ def start_server(applications, port=8080, host='',
157157
_, port = _setup_server(webio_handler=handler, port=port, host=host, static_dir=static_dir,
158158
max_buffer_size=parse_file_size(max_payload_size), **tornado_app_settings)
159159

160-
print('Listen on %s:%s' % (host or '0.0.0.0', port))
160+
print_listen_address(host, port)
161161
if auto_open_webbrowser:
162162
tornado.ioloop.IOLoop.current().spawn_callback(open_webbrowser_on_server_started, host or 'localhost', port)
163163

pywebio/platform/utils.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import fnmatch
22
import json
3+
import socket
34
import urllib.parse
45
from collections import defaultdict
56
from collections import namedtuple
67
from collections.abc import Mapping, Sequence
8+
from functools import lru_cache
79
from functools import partial
810
from os import path, environ
911

1012
from tornado import template
11-
from functools import lru_cache
1213

1314
from ..__version__ import __version__ as version
1415
from ..exceptions import PyWebIOWarning
@@ -275,6 +276,48 @@ def deserialize_binary_event(data: bytes):
275276
return event
276277

277278

279+
def get_interface_ip(family: socket.AddressFamily) -> str:
280+
"""Get the IP address of an external interface. Used when binding to
281+
0.0.0.0 or :: to show a more useful URL.
282+
283+
Copy from https://github.com/pallets/werkzeug/blob/df7492ab66aaced5eea964a58309caaadb1e8903/src/werkzeug/serving.py
284+
Under BSD-3-Clause License
285+
"""
286+
# arbitrary private address
287+
host = "fd31:f903:5ab5:1::1" if family == socket.AF_INET6 else "10.253.155.219"
288+
289+
with socket.socket(family, socket.SOCK_DGRAM) as s:
290+
try:
291+
s.connect((host, 58162))
292+
except OSError:
293+
return "::1" if family == socket.AF_INET6 else "127.0.0.1"
294+
295+
return s.getsockname()[0] # type: ignore
296+
297+
298+
def print_listen_address(host, port):
299+
if not host:
300+
host = '0.0.0.0'
301+
302+
all_address = False
303+
if host == "0.0.0.0":
304+
all_address = True
305+
host = get_interface_ip(socket.AF_INET)
306+
elif host == "::":
307+
all_address = True
308+
host = get_interface_ip(socket.AF_INET6)
309+
310+
if ':' in host: # ipv6
311+
host = '[%s]' % host
312+
313+
if all_address:
314+
print('Running on all addresses.')
315+
print('Use http://%s:%s/ to access the application' % (host, port))
316+
else:
317+
print('Running on http://%s:%s/' % (host, port))
318+
319+
320+
278321
def seo(title, description=None, app=None):
279322
"""Set the SEO information of the PyWebIO application (web page information provided when indexed by search engines)
280323

0 commit comments

Comments
 (0)