testapp.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. """A small application that can be used to test a WSGI server and check
  2. it for WSGI compliance.
  3. """
  4. import base64
  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. if t.TYPE_CHECKING:
  14. from _typeshed.wsgi import StartResponse
  15. from _typeshed.wsgi import WSGIEnvironment
  16. logo = Response(
  17. base64.b64decode(
  18. """
  19. R0lGODlhoACgAOMIAAEDACwpAEpCAGdgAJaKAM28AOnVAP3rAP/////////
  20. //////////////////////yH5BAEKAAgALAAAAACgAKAAAAT+EMlJq704680R+F0ojmRpnuj0rWnrv
  21. nB8rbRs33gu0bzu/0AObxgsGn3D5HHJbCUFyqZ0ukkSDlAidctNFg7gbI9LZlrBaHGtzAae0eloe25
  22. 7w9EDOX2fst/xenyCIn5/gFqDiVVDV4aGeYiKkhSFjnCQY5OTlZaXgZp8nJ2ekaB0SQOjqphrpnOiq
  23. ncEn65UsLGytLVmQ6m4sQazpbtLqL/HwpnER8bHyLrLOc3Oz8PRONPU1crXN9na263dMt/g4SzjMeX
  24. m5yDpLqgG7OzJ4u8lT/P69ej3JPn69kHzN2OIAHkB9RUYSFCFQYQJFTIkCDBiwoXWGnowaLEjRm7+G
  25. p9A7Hhx4rUkAUaSLJlxHMqVMD/aSycSZkyTplCqtGnRAM5NQ1Ly5OmzZc6gO4d6DGAUKA+hSocWYAo
  26. SlM6oUWX2O/o0KdaVU5vuSQLAa0ADwQgMEMB2AIECZhVSnTno6spgbtXmHcBUrQACcc2FrTrWS8wAf
  27. 78cMFBgwIBgbN+qvTt3ayikRBk7BoyGAGABAdYyfdzRQGV3l4coxrqQ84GpUBmrdR3xNIDUPAKDBSA
  28. ADIGDhhqTZIWaDcrVX8EsbNzbkvCOxG8bN5w8ly9H8jyTJHC6DFndQydbguh2e/ctZJFXRxMAqqPVA
  29. tQH5E64SPr1f0zz7sQYjAHg0In+JQ11+N2B0XXBeeYZgBZFx4tqBToiTCPv0YBgQv8JqA6BEf6RhXx
  30. w1ENhRBnWV8ctEX4Ul2zc3aVGcQNC2KElyTDYyYUWvShdjDyMOGMuFjqnII45aogPhz/CodUHFwaDx
  31. lTgsaOjNyhGWJQd+lFoAGk8ObghI0kawg+EV5blH3dr+digkYuAGSaQZFHFz2P/cTaLmhF52QeSb45
  32. Jwxd+uSVGHlqOZpOeJpCFZ5J+rkAkFjQ0N1tah7JJSZUFNsrkeJUJMIBi8jyaEKIhKPomnC91Uo+NB
  33. yyaJ5umnnpInIFh4t6ZSpGaAVmizqjpByDegYl8tPE0phCYrhcMWSv+uAqHfgH88ak5UXZmlKLVJhd
  34. dj78s1Fxnzo6yUCrV6rrDOkluG+QzCAUTbCwf9SrmMLzK6p+OPHx7DF+bsfMRq7Ec61Av9i6GLw23r
  35. idnZ+/OO0a99pbIrJkproCQMA17OPG6suq3cca5ruDfXCCDoS7BEdvmJn5otdqscn+uogRHHXs8cbh
  36. EIfYaDY1AkrC0cqwcZpnM6ludx72x0p7Fo/hZAcpJDjax0UdHavMKAbiKltMWCF3xxh9k25N/Viud8
  37. ba78iCvUkt+V6BpwMlErmcgc502x+u1nSxJSJP9Mi52awD1V4yB/QHONsnU3L+A/zR4VL/indx/y64
  38. gqcj+qgTeweM86f0Qy1QVbvmWH1D9h+alqg254QD8HJXHvjQaGOqEqC22M54PcftZVKVSQG9jhkv7C
  39. JyTyDoAJfPdu8v7DRZAxsP/ky9MJ3OL36DJfCFPASC3/aXlfLOOON9vGZZHydGf8LnxYJuuVIbl83y
  40. Az5n/RPz07E+9+zw2A2ahz4HxHo9Kt79HTMx1Q7ma7zAzHgHqYH0SoZWyTuOLMiHwSfZDAQTn0ajk9
  41. YQqodnUYjByQZhZak9Wu4gYQsMyEpIOAOQKze8CmEF45KuAHTvIDOfHJNipwoHMuGHBnJElUoDmAyX
  42. c2Qm/R8Ah/iILCCJOEokGowdhDYc/yoL+vpRGwyVSCWFYZNljkhEirGXsalWcAgOdeAdoXcktF2udb
  43. qbUhjWyMQxYO01o6KYKOr6iK3fE4MaS+DsvBsGOBaMb0Y6IxADaJhFICaOLmiWTlDAnY1KzDG4ambL
  44. cWBA8mUzjJsN2KjSaSXGqMCVXYpYkj33mcIApyhQf6YqgeNAmNvuC0t4CsDbSshZJkCS1eNisKqlyG
  45. cF8G2JeiDX6tO6Mv0SmjCa3MFb0bJaGPMU0X7c8XcpvMaOQmCajwSeY9G0WqbBmKv34DsMIEztU6Y2
  46. KiDlFdt6jnCSqx7Dmt6XnqSKaFFHNO5+FmODxMCWBEaco77lNDGXBM0ECYB/+s7nKFdwSF5hgXumQe
  47. EZ7amRg39RHy3zIjyRCykQh8Zo2iviRKyTDn/zx6EefptJj2Cw+Ep2FSc01U5ry4KLPYsTyWnVGnvb
  48. UpyGlhjBUljyjHhWpf8OFaXwhp9O4T1gU9UeyPPa8A2l0p1kNqPXEVRm1AOs1oAGZU596t6SOR2mcB
  49. Oco1srWtkaVrMUzIErrKri85keKqRQYX9VX0/eAUK1hrSu6HMEX3Qh2sCh0q0D2CtnUqS4hj62sE/z
  50. aDs2Sg7MBS6xnQeooc2R2tC9YrKpEi9pLXfYXp20tDCpSP8rKlrD4axprb9u1Df5hSbz9QU0cRpfgn
  51. kiIzwKucd0wsEHlLpe5yHXuc6FrNelOl7pY2+11kTWx7VpRu97dXA3DO1vbkhcb4zyvERYajQgAADs
  52. ="""
  53. ),
  54. mimetype="image/png",
  55. )
  56. TEMPLATE = """\
  57. <!doctype html>
  58. <html lang=en>
  59. <title>WSGI Information</title>
  60. <style type="text/css">
  61. @import url(https://fonts.googleapis.com/css?family=Ubuntu);
  62. body { font-family: 'Lucida Grande', 'Lucida Sans Unicode', 'Geneva',
  63. 'Verdana', sans-serif; background-color: white; color: #000;
  64. font-size: 15px; text-align: center; }
  65. #logo { float: right; padding: 0 0 10px 10px; }
  66. div.box { text-align: left; width: 45em; margin: auto; padding: 50px 0;
  67. background-color: white; }
  68. h1, h2 { font-family: 'Ubuntu', 'Lucida Grande', 'Lucida Sans Unicode',
  69. 'Geneva', 'Verdana', sans-serif; font-weight: normal; }
  70. h1 { margin: 0 0 30px 0; }
  71. h2 { font-size: 1.4em; margin: 1em 0 0.5em 0; }
  72. table { width: 100%%; border-collapse: collapse; border: 1px solid #AFC5C9 }
  73. table th { background-color: #AFC1C4; color: white; font-size: 0.72em;
  74. font-weight: normal; width: 18em; vertical-align: top;
  75. padding: 0.5em 0 0.1em 0.5em; }
  76. table td { border: 1px solid #AFC5C9; padding: 0.1em 0 0.1em 0.5em; }
  77. code { font-family: 'Consolas', 'Monaco', 'Bitstream Vera Sans Mono',
  78. monospace; font-size: 0.7em; }
  79. ul li { line-height: 1.5em; }
  80. ul.path { font-size: 0.7em; margin: 0 -30px; padding: 8px 30px;
  81. list-style: none; background: #E8EFF0; }
  82. ul.path li { line-height: 1.6em; }
  83. li.virtual { color: #999; text-decoration: underline; }
  84. li.exp { background: white; }
  85. </style>
  86. <div class="box">
  87. <img src="?resource=logo" id="logo" alt="[The Werkzeug Logo]" />
  88. <h1>WSGI Information</h1>
  89. <p>
  90. This page displays all available information about the WSGI server and
  91. the underlying Python interpreter.
  92. <h2 id="python-interpreter">Python Interpreter</h2>
  93. <table>
  94. <tr>
  95. <th>Python Version
  96. <td>%(python_version)s
  97. <tr>
  98. <th>Platform
  99. <td>%(platform)s [%(os)s]
  100. <tr>
  101. <th>API Version
  102. <td>%(api_version)s
  103. <tr>
  104. <th>Byteorder
  105. <td>%(byteorder)s
  106. <tr>
  107. <th>Werkzeug Version
  108. <td>%(werkzeug_version)s
  109. </table>
  110. <h2 id="wsgi-environment">WSGI Environment</h2>
  111. <table>%(wsgi_env)s</table>
  112. <h2 id="installed-eggs">Installed Eggs</h2>
  113. <p>
  114. The following python packages were installed on the system as
  115. Python eggs:
  116. <ul>%(python_eggs)s</ul>
  117. <h2 id="sys-path">System Path</h2>
  118. <p>
  119. The following paths are the current contents of the load path. The
  120. following entries are looked up for Python packages. Note that not
  121. all items in this path are folders. Gray and underlined items are
  122. entries pointing to invalid resources or used by custom import hooks
  123. such as the zip importer.
  124. <p>
  125. Items with a bright background were expanded for display from a relative
  126. path. If you encounter such paths in the output you might want to check
  127. your setup as relative paths are usually problematic in multithreaded
  128. environments.
  129. <ul class="path">%(sys_path)s</ul>
  130. </div>
  131. """
  132. def iter_sys_path() -> t.Iterator[t.Tuple[str, bool, bool]]:
  133. if os.name == "posix":
  134. def strip(x: str) -> str:
  135. prefix = os.path.expanduser("~")
  136. if x.startswith(prefix):
  137. x = f"~{x[len(prefix) :]}"
  138. return x
  139. else:
  140. def strip(x: str) -> str:
  141. return x
  142. cwd = os.path.abspath(os.getcwd())
  143. for item in sys.path:
  144. path = os.path.join(cwd, item or os.path.curdir)
  145. yield strip(os.path.normpath(path)), not os.path.isdir(path), path != item
  146. def render_testapp(req: Request) -> bytes:
  147. try:
  148. import pkg_resources
  149. except ImportError:
  150. eggs: t.Iterable[t.Any] = ()
  151. else:
  152. eggs = sorted(
  153. pkg_resources.working_set,
  154. key=lambda x: x.project_name.lower(), # type: ignore
  155. )
  156. python_eggs = []
  157. for egg in eggs:
  158. try:
  159. version = egg.version
  160. except (ValueError, AttributeError):
  161. version = "unknown"
  162. python_eggs.append(
  163. f"<li>{escape(egg.project_name)} <small>[{escape(version)}]</small>"
  164. )
  165. wsgi_env = []
  166. sorted_environ = sorted(req.environ.items(), key=lambda x: repr(x[0]).lower())
  167. for key, value in sorted_environ:
  168. value = "".join(wrap(str(escape(repr(value)))))
  169. wsgi_env.append(f"<tr><th>{escape(key)}<td><code>{value}</code>")
  170. sys_path = []
  171. for item, virtual, expanded in iter_sys_path():
  172. class_ = []
  173. if virtual:
  174. class_.append("virtual")
  175. if expanded:
  176. class_.append("exp")
  177. class_ = f' class="{" ".join(class_)}"' if class_ else ""
  178. sys_path.append(f"<li{class_}>{escape(item)}")
  179. return (
  180. TEMPLATE
  181. % {
  182. "python_version": "<br>".join(escape(sys.version).splitlines()),
  183. "platform": escape(sys.platform),
  184. "os": escape(os.name),
  185. "api_version": sys.api_version,
  186. "byteorder": sys.byteorder,
  187. "werkzeug_version": _werkzeug_version,
  188. "python_eggs": "\n".join(python_eggs),
  189. "wsgi_env": "\n".join(wsgi_env),
  190. "sys_path": "\n".join(sys_path),
  191. }
  192. ).encode("utf-8")
  193. def test_app(
  194. environ: "WSGIEnvironment", start_response: "StartResponse"
  195. ) -> t.Iterable[bytes]:
  196. """Simple test application that dumps the environment. You can use
  197. it to check if Werkzeug is working properly:
  198. .. sourcecode:: pycon
  199. >>> from werkzeug.serving import run_simple
  200. >>> from werkzeug.testapp import test_app
  201. >>> run_simple('localhost', 3000, test_app)
  202. * Running on http://localhost:3000/
  203. The application displays important information from the WSGI environment,
  204. the Python interpreter and the installed libraries.
  205. """
  206. req = Request(environ, populate_request=False)
  207. if req.args.get("resource") == "logo":
  208. response = logo
  209. else:
  210. response = Response(render_testapp(req), mimetype="text/html")
  211. return response(environ, start_response)
  212. if __name__ == "__main__":
  213. from .serving import run_simple
  214. run_simple("localhost", 5000, test_app, use_reloader=True)