This basic handler explains how to return actual content, using a variation on the "Hello, World!" example (this is Python, after all).
1 """
2 A "Hello, World!" example for a minimal PythonHandler.
3 """
4
5 from mod_python import apache
6
7 def handler(req):
8 req.content_type = "text/plain"
9 req.write("Hello, Bruce!")
10 return apache.OK
This example is a little more fully formed than the MostMinimalRequestHandler.
We've included a docstring:
1 """
2 A "Hello, World!" example for a minimal PythonHandler.
3 """
We're importing an external module:
1 from mod_python import apache
We're declaring the MIME type that we want the server to announce in the Content-Type header:
1 def handler(req):
2 req.content_type = "text/plain"
We're writing our content out as a string:
1 req.write("Hello, Bruce!")
And we're using a constant conveniently defined as zero (0) in the module we've imported:
1 return apache.OK
The visitor's browser will display a simple text string:
Hello, Bruce!
The server response will look something like this (depending on server configuration):
HTTP/1.x 200 OK Date: Sat, 02 Dec 2006 18:18:34 GMT Server: Apache Vary: Accept-Encoding Content-Encoding: gzip Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: text/plain
This example demonstrates some of the basic housekeeping that must be done to return real content. It's also useful to understand that this content can be of any MIME type. Of course, the most common type will be text, and most frequently that will be HTML (at least until application/xhtml+xml is more universally supported by browsers).