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