Compare commits
5 Commits
motd_addre
...
bind_excep
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69d493abce | ||
|
|
e6c0f3990c | ||
|
|
ab19d7b5dd | ||
|
|
abdb5cb531 | ||
|
|
b9b54a2eda |
@@ -49,7 +49,7 @@ from sanic.application.motd import MOTD
|
|||||||
from sanic.application.state import ApplicationServerInfo, Mode, ServerStage
|
from sanic.application.state import ApplicationServerInfo, Mode, ServerStage
|
||||||
from sanic.base.meta import SanicMeta
|
from sanic.base.meta import SanicMeta
|
||||||
from sanic.compat import OS_IS_WINDOWS, StartMethod
|
from sanic.compat import OS_IS_WINDOWS, StartMethod
|
||||||
from sanic.exceptions import ServerKilled
|
from sanic.exceptions import SanicException, ServerKilled
|
||||||
from sanic.helpers import Default, _default, is_atty
|
from sanic.helpers import Default, _default, is_atty
|
||||||
from sanic.http.constants import HTTP
|
from sanic.http.constants import HTTP
|
||||||
from sanic.http.tls import get_ssl_context, process_to_context
|
from sanic.http.tls import get_ssl_context, process_to_context
|
||||||
@@ -657,9 +657,10 @@ class StartupMixin(metaclass=SanicMeta):
|
|||||||
def get_server_location(
|
def get_server_location(
|
||||||
server_settings: Optional[Dict[str, Any]] = None
|
server_settings: Optional[Dict[str, Any]] = None
|
||||||
) -> str:
|
) -> str:
|
||||||
|
serve_location = ""
|
||||||
proto = "http"
|
proto = "http"
|
||||||
if not server_settings:
|
if not server_settings:
|
||||||
return ""
|
return serve_location
|
||||||
|
|
||||||
host = server_settings["host"]
|
host = server_settings["host"]
|
||||||
port = server_settings["port"]
|
port = server_settings["port"]
|
||||||
@@ -667,33 +668,16 @@ class StartupMixin(metaclass=SanicMeta):
|
|||||||
if server_settings.get("ssl") is not None:
|
if server_settings.get("ssl") is not None:
|
||||||
proto = "https"
|
proto = "https"
|
||||||
if server_settings.get("unix"):
|
if server_settings.get("unix"):
|
||||||
return f'{server_settings["unix"]} {proto}://localhost'
|
serve_location = f'{server_settings["unix"]} {proto}://...'
|
||||||
if server_settings.get("sock"):
|
elif server_settings.get("sock"):
|
||||||
host, port, *_ = server_settings["sock"].getsockname()
|
host, port, *_ = server_settings["sock"].getsockname()
|
||||||
if not host or not port:
|
|
||||||
return ""
|
|
||||||
|
|
||||||
# colon(:) is legal for a host only in an ipv6 address
|
if not serve_location and host and port:
|
||||||
url_host = f"[{host}]" if ":" in host else host
|
# colon(:) is legal for a host only in an ipv6 address
|
||||||
url_port = (
|
display_host = f"[{host}]" if ":" in host else host
|
||||||
""
|
serve_location = f"{proto}://{display_host}:{port}"
|
||||||
if (
|
|
||||||
(proto == "https" and port == 443)
|
|
||||||
or (proto == "http" and port == 80)
|
|
||||||
)
|
|
||||||
else f":{port}"
|
|
||||||
)
|
|
||||||
|
|
||||||
special = {
|
return serve_location
|
||||||
"127.0.0.1": "IPv4",
|
|
||||||
"0.0.0.0": "IPv4 wildcard",
|
|
||||||
"::1": "IPv6",
|
|
||||||
"::": "IPv6 wildcard",
|
|
||||||
}.get(host, "")
|
|
||||||
if special:
|
|
||||||
return f"({special}) {proto}://localhost{url_port}"
|
|
||||||
|
|
||||||
return f"{proto}://{url_host}{url_port}"
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_address(
|
def get_address(
|
||||||
@@ -896,12 +880,18 @@ class StartupMixin(metaclass=SanicMeta):
|
|||||||
manager.run()
|
manager.run()
|
||||||
except ServerKilled:
|
except ServerKilled:
|
||||||
exit_code = 1
|
exit_code = 1
|
||||||
|
except SanicException as e:
|
||||||
|
exit_code = 1
|
||||||
|
kwargs = primary_server_info.settings
|
||||||
|
if e.quiet:
|
||||||
|
error_logger.error(str(e))
|
||||||
|
else:
|
||||||
|
raise
|
||||||
except BaseException:
|
except BaseException:
|
||||||
kwargs = primary_server_info.settings
|
kwargs = primary_server_info.settings
|
||||||
error_logger.exception(
|
error_logger.exception(
|
||||||
"Experienced exception while trying to serve"
|
"Experienced exception while trying to serve"
|
||||||
)
|
)
|
||||||
raise
|
|
||||||
finally:
|
finally:
|
||||||
logger.info("Server Stopped")
|
logger.info("Server Stopped")
|
||||||
for app in apps:
|
for app in apps:
|
||||||
|
|||||||
@@ -47,10 +47,10 @@ def bind_unix_socket(path: str, *, mode=0o666, backlog=100) -> socket.socket:
|
|||||||
path = os.path.abspath(path)
|
path = os.path.abspath(path)
|
||||||
folder = os.path.dirname(path)
|
folder = os.path.dirname(path)
|
||||||
if not os.path.isdir(folder):
|
if not os.path.isdir(folder):
|
||||||
raise FileNotFoundError(f"Socket folder does not exist: {folder}")
|
raise FileNotFoundError("Socket folder does not exist")
|
||||||
try:
|
try:
|
||||||
if not stat.S_ISSOCK(os.stat(path, follow_symlinks=False).st_mode):
|
if not stat.S_ISSOCK(os.stat(path, follow_symlinks=False).st_mode):
|
||||||
raise FileExistsError(f"Existing file is not a socket: {path}")
|
raise FileExistsError("Existing file is not a socket")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
# Create new socket with a random temporary name
|
# Create new socket with a random temporary name
|
||||||
@@ -103,7 +103,10 @@ def configure_socket(
|
|||||||
unix = server_settings["unix"]
|
unix = server_settings["unix"]
|
||||||
backlog = server_settings["backlog"]
|
backlog = server_settings["backlog"]
|
||||||
if unix:
|
if unix:
|
||||||
sock = bind_unix_socket(unix, backlog=backlog)
|
try:
|
||||||
|
sock = bind_unix_socket(unix, backlog=backlog)
|
||||||
|
except OSError as e:
|
||||||
|
raise ServerError(f"Error binding {unix}: {e}", quiet=True)
|
||||||
server_settings["unix"] = unix
|
server_settings["unix"] = unix
|
||||||
if sock is None:
|
if sock is None:
|
||||||
try:
|
try:
|
||||||
@@ -112,6 +115,17 @@ def configure_socket(
|
|||||||
server_settings["port"],
|
server_settings["port"],
|
||||||
backlog=backlog,
|
backlog=backlog,
|
||||||
)
|
)
|
||||||
|
except PermissionError:
|
||||||
|
p = server_settings["port"]
|
||||||
|
if not p or p >= 1024:
|
||||||
|
raise
|
||||||
|
addr = f"{server_settings['host']}:{p}"
|
||||||
|
error = ServerError(
|
||||||
|
f"Permission denied binding to {addr}.\n\n"
|
||||||
|
"Use `sudo sanic` to run on a privileged port.\n"
|
||||||
|
)
|
||||||
|
error.quiet = True
|
||||||
|
raise error
|
||||||
except OSError as e: # no cov
|
except OSError as e: # no cov
|
||||||
error = ServerError(
|
error = ServerError(
|
||||||
f"Sanic server could not start: {e}.\n\n"
|
f"Sanic server could not start: {e}.\n\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user