comparison wiki/server/test.wsgi @ 4034:8ced48dc0a3d

added test.wsgi script
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 24 Aug 2008 20:51:37 +0200
parents
children fbf92e24f4d2
comparison
equal deleted inserted replaced
4033:404220520c63 4034:8ced48dc0a3d
1 #!/usr/bin/env python
2 """
3 A simple WSGI test application.
4
5 Its main purpose is to show that WSGI support works (meaning that the
6 web server and the WSGI adaptor / support module are configured correctly).
7
8 As a nice plus, it outputs some interesting system / WSGI values as a nice
9 HTML table.
10
11 The main use of this script will be using the WSGI "application" defined
12 below within your production WSGI environment. You will use some code similar
13 to what you see at the end of this script to use the application from that
14 environment. For the special case of apache2/mod_wsgi, it shoud be possible
15 to directly use this file.
16
17 If you start this script from the commandline either with python2.5 or with
18 and older python + wsgiref module installed, it will serve the content on
19 http://localhost:8000/ - this is mainly for debugging THIS script.
20
21 @copyright: 2008 by MoinMoin:ThomasWaldmann
22 @license: GPL v2 or later, see COPYING for details.
23 """
24 import os.path
25 import os
26 import sys
27
28 try:
29 __file__
30 except NameError:
31 __file__ = '?'
32
33 html_template = """\
34 <html>
35 <head>
36 <title>WSGI Test Script working!</title>
37 </head>
38 <body>
39 <table border=1>
40 <tr><th colspan=2>1. System Information</th></tr>
41 <tr><td>Python</td><td>%(python_version)s</td></tr>
42 <tr><td>Platform</td><td>%(platform)s</td></tr>
43 <tr><td>Absolute path of this script</td><td>%(abs_path)s</td></tr>
44 <tr><td>Filename</td><td>%(filename)s</td></tr>
45 <tr><th colspan=2>2. WSGI Environment</th></tr>
46 %(wsgi_env)s
47 </table>
48 </body>
49 </html>
50 """
51
52 row_template = " <tr><td>%s</td><td>%r</td></tr>"
53
54
55 def application(environ, start_response):
56 """ The WSGI test application """
57 # emit status / headers
58 status = "200 OK"
59 headers = [('Content-Type', 'text/html'), ]
60 start_response(status, headers)
61
62 # assemble and return content
63 content = html_template % {
64 'python_version': sys.version,
65 'platform': sys.platform,
66 'abs_path': os.path.abspath('.'),
67 'filename': __file__,
68 'python path': repr(sys.path),
69 'wsgi_env': '\n'.join([row_template % item for item in environ.items()]),
70 }
71 return [content]
72
73
74 if __name__ == '__main__':
75 # this runs when script is started directly from commandline
76 try:
77 # create a simple WSGI server and run the application
78 from wsgiref import simple_server
79 httpd = simple_server.WSGIServer(('', 8000), simple_server.WSGIRequestHandler)
80 httpd.set_app(application)
81 httpd.serve_forever()
82 except ImportError:
83 # wsgiref not installed, just output html to stdout
84 for content in application({}, lambda status, headers: None):
85 print content
86