1.14.1.5 The PythonWeb WSGI Server

A WSGI server has to be able to convert a URL to a path on a drive, find the application named application within the file specified and call it, passing the application a dictionary of environmental variables and a start_response function to set the status of the application and send the HTTP headers.

Note: As we have seen the object named application may not be an application at all, it may in fact be a chain of middleware components and an application, but the WSGI server treats it in the same way because, as we have already seen, applications with middleware stacks behave in exactly the same way as an application on its own.

The Python Web Modules come with just such a WSGI server named WSGIServer.py and available in the scripts directory of the Web Modules distribution.

To use the WSGI server simply run the WSGIServer.py file from the command line by executing the following:

> python WSGIServer.py

A sample WSGI application should be available by http://localhost:8000/simple with a web browser.

WSGIServer.py also takes a series of arguments to customise its behaviour. These can be viewed by running python WSGIServer.py -h at the command line.

Warning: Since the WSGIServer loads all the application code when it starts, if you make changes to the samples the server will need to be restarted before the changes will take effect.

The WSGI Server also supports a special debug mode. If your application raises a web.error.Breakpoint exception, the server will not handle the request but instead will give a debug prompt so that you can debug the variables at the breakpoint.

import web.wsgi

class simpleApp(web.wsgi.base.BaseApplication):
    def start(self):
        import web.error
        value = 5
        raise web.error.Breakpoint('Test Exception')

application = simpleApp()

With the WSGI Server running test this example by visiting http://localhost:8000/debug with a web browser. You will see a Handling Breakpoint message at the server prompt (nothing will be displayed in the browser though). Press Enter.

You can debug the code as follows and then type exit to exit the prompt.

debug> value
5
debug> exit