Skip to content

Commit

Permalink
always show dev server warning
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Aug 8, 2022
1 parent 98e7739 commit 09a3066
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -14,6 +14,8 @@ Unreleased
``import as`` syntax for explicitly re-exporting public attributes.
:pr:`2493`
- Parsing of some invalid header characters is more robust. :pr:`2494`
- When starting the development server, a warning not to use it in a
production deployment is always shown. :issue:`2480`


Version 2.2.1
Expand Down
78 changes: 41 additions & 37 deletions src/werkzeug/serving.py
Expand Up @@ -408,21 +408,20 @@ def log_request(

code = str(code)

if _log_add_style:
if code[0] == "1": # 1xx - Informational
msg = _ansi_style(msg, "bold")
elif code == "200": # 2xx - Success
pass
elif code == "304": # 304 - Resource Not Modified
msg = _ansi_style(msg, "cyan")
elif code[0] == "3": # 3xx - Redirection
msg = _ansi_style(msg, "green")
elif code == "404": # 404 - Resource Not Found
msg = _ansi_style(msg, "yellow")
elif code[0] == "4": # 4xx - Client Error
msg = _ansi_style(msg, "bold", "red")
else: # 5xx, or any other response
msg = _ansi_style(msg, "bold", "magenta")
if code[0] == "1": # 1xx - Informational
msg = _ansi_style(msg, "bold")
elif code == "200": # 2xx - Success
pass
elif code == "304": # 304 - Resource Not Modified
msg = _ansi_style(msg, "cyan")
elif code[0] == "3": # 3xx - Redirection
msg = _ansi_style(msg, "green")
elif code == "404": # 404 - Resource Not Found
msg = _ansi_style(msg, "yellow")
elif code[0] == "4": # 4xx - Client Error
msg = _ansi_style(msg, "bold", "red")
else: # 5xx, or any other response
msg = _ansi_style(msg, "bold", "magenta")

self.log("info", '"%s" %s %s', msg, code, size)

Expand All @@ -441,6 +440,9 @@ def log(self, type: str, message: str, *args: t.Any) -> None:


def _ansi_style(value: str, *styles: str) -> str:
if not _log_add_style:
return value

codes = {
"bold": 1,
"red": 31,
Expand Down Expand Up @@ -752,36 +754,37 @@ def handle_error(

def log_startup(self) -> None:
"""Show information about the address when starting the server."""
dev_warning = (
"WARNING: This is a development server. Do not use it in a production"
" deployment. Use a production WSGI server instead."
)
dev_warning = _ansi_style(dev_warning, "bold", "red")
messages = [dev_warning]

if self.address_family == af_unix:
_log("info", f" * Running on {self.host} (Press CTRL+C to quit)")
messages.append(f" * Running on {self.host}")
else:
scheme = "http" if self.ssl_context is None else "https"
messages = []
all_addresses_message = (
f" * Running on all addresses ({self.host})\n"
" WARNING: This is a development server. Do not use it in"
" a production deployment."
)
display_hostname = self.host

if self.host == "0.0.0.0":
messages.append(all_addresses_message)
messages.append(f" * Running on {scheme}://127.0.0.1:{self.port}")
display_hostname = get_interface_ip(socket.AF_INET)
elif self.host == "::":
messages.append(all_addresses_message)
messages.append(f" * Running on {scheme}://[::1]:{self.port}")
display_hostname = get_interface_ip(socket.AF_INET6)
else:
display_hostname = self.host
if self.host in {"0.0.0.0", "::"}:
messages.append(f" * Running on all addresses ({self.host})")

if self.host == "0.0.0.0":
localhost = "127.0.0.1"
display_hostname = get_interface_ip(socket.AF_INET)
else:
localhost = "[::1]"
display_hostname = get_interface_ip(socket.AF_INET6)

messages.append(f" * Running on {scheme}://{localhost}:{self.port}")

if ":" in display_hostname:
display_hostname = f"[{display_hostname}]"

messages.append(
f" * Running on {scheme}://{display_hostname}:{self.port}"
" (Press CTRL+C to quit)"
)
_log("info", "\n".join(messages))
messages.append(f" * Running on {scheme}://{display_hostname}:{self.port}")

_log("info", "\n".join(messages))


class ThreadedWSGIServer(socketserver.ThreadingMixIn, BaseWSGIServer):
Expand Down Expand Up @@ -1076,6 +1079,7 @@ def run_simple(

if not is_running_from_reloader():
srv.log_startup()
_log("info", _ansi_style("Press CTRL+C to quit", "yellow"))

if use_reloader:
from ._reloader import run_with_reloader
Expand Down

0 comments on commit 09a3066

Please sign in to comment.