testapp.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. """A small application that can be used to test a WSGI server and check
  2. it for WSGI compliance.
  3. """
  4. from __future__ import annotations
  5. import os
  6. import sys
  7. import typing as t
  8. from textwrap import wrap
  9. from markupsafe import escape
  10. from . import __version__ as _werkzeug_version
  11. from .wrappers.request import Request
  12. from .wrappers.response import Response
  13. TEMPLATE = """\
  14. <!doctype html>
  15. <html lang=en>
  16. <title>WSGI Information</title>
  17. <style type="text/css">
  18. @import url(https://fonts.googleapis.com/css?family=Ubuntu);
  19. body { font-family: 'Lucida Grande', 'Lucida Sans Unicode', 'Geneva',
  20. 'Verdana', sans-serif; background-color: white; color: #000;
  21. font-size: 15px; text-align: center; }
  22. div.box { text-align: left; width: 45em; margin: auto; padding: 50px 0;
  23. background-color: white; }
  24. h1, h2 { font-family: 'Ubuntu', 'Lucida Grande', 'Lucida Sans Unicode',
  25. 'Geneva', 'Verdana', sans-serif; font-weight: normal; }
  26. h1 { margin: 0 0 30px 0; }
  27. h2 { font-size: 1.4em; margin: 1em 0 0.5em 0; }
  28. table { width: 100%%; border-collapse: collapse; border: 1px solid #AFC5C9 }
  29. table th { background-color: #AFC1C4; color: white; font-size: 0.72em;
  30. font-weight: normal; width: 18em; vertical-align: top;
  31. padding: 0.5em 0 0.1em 0.5em; }
  32. table td { border: 1px solid #AFC5C9; padding: 0.1em 0 0.1em 0.5em; }
  33. code { font-family: 'Consolas', 'Monaco', 'Bitstream Vera Sans Mono',
  34. monospace; font-size: 0.7em; }
  35. ul li { line-height: 1.5em; }
  36. ul.path { font-size: 0.7em; margin: 0 -30px; padding: 8px 30px;
  37. list-style: none; background: #E8EFF0; }
  38. ul.path li { line-height: 1.6em; }
  39. li.virtual { color: #999; text-decoration: underline; }
  40. li.exp { background: white; }
  41. </style>
  42. <div class="box">
  43. <h1>WSGI Information</h1>
  44. <p>
  45. This page displays all available information about the WSGI server and
  46. the underlying Python interpreter.
  47. <h2 id="python-interpreter">Python Interpreter</h2>
  48. <table>
  49. <tr>
  50. <th>Python Version
  51. <td>%(python_version)s
  52. <tr>
  53. <th>Platform
  54. <td>%(platform)s [%(os)s]
  55. <tr>
  56. <th>API Version
  57. <td>%(api_version)s
  58. <tr>
  59. <th>Byteorder
  60. <td>%(byteorder)s
  61. <tr>
  62. <th>Werkzeug Version
  63. <td>%(werkzeug_version)s
  64. </table>
  65. <h2 id="wsgi-environment">WSGI Environment</h2>
  66. <table>%(wsgi_env)s</table>
  67. <h2 id="installed-eggs">Installed Eggs</h2>
  68. <p>
  69. The following python packages were installed on the system as
  70. Python eggs:
  71. <ul>%(python_eggs)s</ul>
  72. <h2 id="sys-path">System Path</h2>
  73. <p>
  74. The following paths are the current contents of the load path. The
  75. following entries are looked up for Python packages. Note that not
  76. all items in this path are folders. Gray and underlined items are
  77. entries pointing to invalid resources or used by custom import hooks
  78. such as the zip importer.
  79. <p>
  80. Items with a bright background were expanded for display from a relative
  81. path. If you encounter such paths in the output you might want to check
  82. your setup as relative paths are usually problematic in multithreaded
  83. environments.
  84. <ul class="path">%(sys_path)s</ul>
  85. </div>
  86. """
  87. def iter_sys_path() -> t.Iterator[tuple[str, bool, bool]]:
  88. if os.name == "posix":
  89. def strip(x: str) -> str:
  90. prefix = os.path.expanduser("~")
  91. if x.startswith(prefix):
  92. x = f"~{x[len(prefix) :]}"
  93. return x
  94. else:
  95. def strip(x: str) -> str:
  96. return x
  97. cwd = os.path.abspath(os.getcwd())
  98. for item in sys.path:
  99. path = os.path.join(cwd, item or os.path.curdir)
  100. yield strip(os.path.normpath(path)), not os.path.isdir(path), path != item
  101. @Request.application
  102. def test_app(req: Request) -> Response:
  103. """Simple test application that dumps the environment. You can use
  104. it to check if Werkzeug is working properly:
  105. .. sourcecode:: pycon
  106. >>> from werkzeug.serving import run_simple
  107. >>> from werkzeug.testapp import test_app
  108. >>> run_simple('localhost', 3000, test_app)
  109. * Running on http://localhost:3000/
  110. The application displays important information from the WSGI environment,
  111. the Python interpreter and the installed libraries.
  112. """
  113. try:
  114. import pkg_resources
  115. except ImportError:
  116. eggs: t.Iterable[t.Any] = ()
  117. else:
  118. eggs = sorted(
  119. pkg_resources.working_set,
  120. key=lambda x: x.project_name.lower(),
  121. )
  122. python_eggs = []
  123. for egg in eggs:
  124. try:
  125. version = egg.version
  126. except (ValueError, AttributeError):
  127. version = "unknown"
  128. python_eggs.append(
  129. f"<li>{escape(egg.project_name)} <small>[{escape(version)}]</small>"
  130. )
  131. wsgi_env = []
  132. sorted_environ = sorted(req.environ.items(), key=lambda x: repr(x[0]).lower())
  133. for key, value in sorted_environ:
  134. value = "".join(wrap(str(escape(repr(value)))))
  135. wsgi_env.append(f"<tr><th>{escape(key)}<td><code>{value}</code>")
  136. sys_path = []
  137. for item, virtual, expanded in iter_sys_path():
  138. class_ = []
  139. if virtual:
  140. class_.append("virtual")
  141. if expanded:
  142. class_.append("exp")
  143. class_ = f' class="{" ".join(class_)}"' if class_ else ""
  144. sys_path.append(f"<li{class_}>{escape(item)}")
  145. context = {
  146. "python_version": "<br>".join(escape(sys.version).splitlines()),
  147. "platform": escape(sys.platform),
  148. "os": escape(os.name),
  149. "api_version": sys.api_version,
  150. "byteorder": sys.byteorder,
  151. "werkzeug_version": _werkzeug_version,
  152. "python_eggs": "\n".join(python_eggs),
  153. "wsgi_env": "\n".join(wsgi_env),
  154. "sys_path": "\n".join(sys_path),
  155. }
  156. return Response(TEMPLATE % context, mimetype="text/html")
  157. if __name__ == "__main__":
  158. from .serving import run_simple
  159. run_simple("localhost", 5000, test_app, use_reloader=True)