Python's Apache interpreter is available as an Apache module, mod_python. This module reduces the time it takes to deliver a given page to a client. It is also capable of a great deal more, including interacting with Apache itself in various powerful ways. This article gives you just a taste of what mod_python can do.
I'm sure many of you are coming from a CGI background and have several CGI scripts sitting around. Fortunately for you, mod_python can emulate a CGI enviroment and run your old scripts. Create a directory called pycgi. Create a .htaccess file and add this to it:
PythonHandler mod_python.cgihandler
We'll now create a simple "CGI" script:
import cgi formData = cgi.FieldStorage() if 'name' in formData: name = formData [ 'name' ].value else: name = 'John Doe'
print 'Content-type: text/html' print print 'Hello, ', name + '.'
You should not, however, rely on this method. It has a few problems, and mod_python offers much more efficient methods of doings things, as I explained at the beginning of the article. Let's not waste a perfectly good Apache module on CGI emulation.
Instead, we'll move on to mod_python's publisher handler. The publisher handler allows us to easily deliver Python powered pages to clients. Instead of having a separate page for everything, mod_python's publisher handler allows us to have a Python function for everything.
Create a directory named pypublish. We'll use this to play around with the publisher handler. Create a .htaccess file with this in it:
PythonHandler mod_python.publisher
Now create a file named test.py and fill it with this:
def index(): return "This is only a test." def peyton(): return "Peyton wrote this."
Obviously, it contains two functions. Let's see how those functions relate to the script's output. Open up your Web browser and head over to the page you just created:
The peyton function is now executed. This interesting feature can be convenient in developing applications.
Now let's make a script that is a bit more complicated. Let's process data from a form - a task vital to functional websites. We'll create a form that asks for the reader's favorite color, the time he or she woke up and his or her favorite language. All this will go in the index function. When the form is submitted, the data will be sent to the process function to be made pretty and displayed. Create a file appropriately named form.py:
def index(): out = "<form method='POST' action='form.py/process'>" out = out + "What is your favorite color?<br>" out = out + "<input type='text' name='color'><br>" out = out + "What time did you wake up this morning?<br>" out = out + "<input type='text' name='waket'><br>" out = out + "What is your favorite language?<br>" out = out + "<select name='flang'><option>Python</option></select><br>" out = out + "<input type='submit' value='Process'>" out = out + "</form>" return out def process ( color, waket, flang ): return "Your favorite color is " + color + ", you woke up at " + waket + " and you like " + flang + "."
The index functon contains no suprises. It just displays form data, so I won't bother going into detail there. The process function, however, is a bit different. As you can see, it takes three arguments. Notice that the three arguments correspond to the names of the form fields. The way the publisher handler handles this is pretty interesting. Try switching around the arguments or even taking out a few (just remember to take out the variable in the return statement, too). The function still executes fine. The publisher handler automatically puts in the correct arguments. Pretty neat, huh?