web.wsgi Functions

runCGI( application)
Wrapper function to enable WSGI applications to run in a CGI environment. application is the WSGI application or middleware to run in a CGI environment.

You may not be in a situation where you have access to a WSGI server. The Python web modules also come with a code to allow WSGI applications and middleware to be run in a CGI environment such as Apache.

If you want to run a WSGI as a CGI application you need to turn it back into one. This can be done very simply by using the middleware component web.wsgi.runCGI as shown below:

#!/usr/bin/env python

# show python where the web modules are
import sys; sys.path.append('../'); sys.path.append('../../../')

def simpleApp(environ, start_response):
    status = '200 OK'
    headers = [('Content-type','text/plain')]
    start_response(status, headers)
    return ['Hello world from simple_application!\n']

import web.wsgi

The application can then be run in a normal CGI webserver.

To test this approach run webserver.py using python webserver.py in the scripts directory and visit http://localhost:8080/doc/src/lib/webserver-web-wsgi-simple-cgi.py to see a sample CGI WSGI application running.

Note: It much faster to execute WSGI applications through a dedicated WSGI server than to run them as CGI scripts. When a CGI script is executed all the Python libraries and modules the script uses need to be loaded into memory and then removed once the script exists. This has to happen for every request so there is an unecessary delay before the WSGI application is even executed. When using a WSGI server the libraries and modules only need to be loaded once and are then available for any subsequent requests so simple web requests can be handled perhaps 10 times faster.

currentURL( environ)
Return the current URL of the WSGI application from the environ dictionary environ