"""A small application that can be used to test a WSGI server and check it for WSGI compliance. """ from __future__ import annotations import os import sys import typing as t from textwrap import wrap from markupsafe import escape from . import __version__ as _werkzeug_version from .wrappers.request import Request from .wrappers.response import Response TEMPLATE = """\ WSGI Information

WSGI Information

This page displays all available information about the WSGI server and the underlying Python interpreter.

Python Interpreter

Python Version %(python_version)s
Platform %(platform)s [%(os)s]
API Version %(api_version)s
Byteorder %(byteorder)s
Werkzeug Version %(werkzeug_version)s

WSGI Environment

%(wsgi_env)s

Installed Eggs

The following python packages were installed on the system as Python eggs:

System Path

The following paths are the current contents of the load path. The following entries are looked up for Python packages. Note that not all items in this path are folders. Gray and underlined items are entries pointing to invalid resources or used by custom import hooks such as the zip importer.

Items with a bright background were expanded for display from a relative path. If you encounter such paths in the output you might want to check your setup as relative paths are usually problematic in multithreaded environments.

""" def iter_sys_path() -> t.Iterator[tuple[str, bool, bool]]: if os.name == "posix": def strip(x: str) -> str: prefix = os.path.expanduser("~") if x.startswith(prefix): x = f"~{x[len(prefix) :]}" return x else: def strip(x: str) -> str: return x cwd = os.path.abspath(os.getcwd()) for item in sys.path: path = os.path.join(cwd, item or os.path.curdir) yield strip(os.path.normpath(path)), not os.path.isdir(path), path != item @Request.application def test_app(req: Request) -> Response: """Simple test application that dumps the environment. You can use it to check if Werkzeug is working properly: .. sourcecode:: pycon >>> from werkzeug.serving import run_simple >>> from werkzeug.testapp import test_app >>> run_simple('localhost', 3000, test_app) * Running on http://localhost:3000/ The application displays important information from the WSGI environment, the Python interpreter and the installed libraries. """ try: import pkg_resources except ImportError: eggs: t.Iterable[t.Any] = () else: eggs = sorted( pkg_resources.working_set, key=lambda x: x.project_name.lower(), ) python_eggs = [] for egg in eggs: try: version = egg.version except (ValueError, AttributeError): version = "unknown" python_eggs.append( f"
  • {escape(egg.project_name)} [{escape(version)}]" ) wsgi_env = [] sorted_environ = sorted(req.environ.items(), key=lambda x: repr(x[0]).lower()) for key, value in sorted_environ: value = "".join(wrap(str(escape(repr(value))))) wsgi_env.append(f"{escape(key)}{value}") sys_path = [] for item, virtual, expanded in iter_sys_path(): class_ = [] if virtual: class_.append("virtual") if expanded: class_.append("exp") class_ = f' class="{" ".join(class_)}"' if class_ else "" sys_path.append(f"{escape(item)}") context = { "python_version": "
    ".join(escape(sys.version).splitlines()), "platform": escape(sys.platform), "os": escape(os.name), "api_version": sys.api_version, "byteorder": sys.byteorder, "werkzeug_version": _werkzeug_version, "python_eggs": "\n".join(python_eggs), "wsgi_env": "\n".join(wsgi_env), "sys_path": "\n".join(sys_path), } return Response(TEMPLATE % context, mimetype="text/html") if __name__ == "__main__": from .serving import run_simple run_simple("localhost", 5000, test_app, use_reloader=True)