1
0

testapp.py 9.1 KB

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