testapp.py 9.2 KB

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