Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

A Brief Introduction to Apache's mod_python Module : Page 3

Giving Apache the ability to run Python scripts opens up a wealth of options for eliminating those old CGI scripts and using Python for your Web development tasks—and gives your scripts a big performance boost into the bargain!


advertisement
The PSP Handler
The PSP Handler lets you embed Python code into Web pages in a fashion that should be immediately recognizable and comfortable to PHP and JSP (Java Server Pages) programmers. To configure an Apache accessible directory for use with the Python Server Pages you would do the following.

<Directory /var/www/html/psp/> AddHandler mod_python .psp PythonHandler mod_python.psp </Directory>

As mentioned above, you can use PSP in a fashion similar to native PHP without using a template system such as Smarty. For example, here's a simple "hello" script using PSP.

<html> <body> <h1><% req.write("Hello!") %></h1> </body> </html>

You could also code the entire getmonth script with PSP, for example:



<title>A simple get month script</title> <body> <% import calendar if form.has_key('month'): month = form['month'] req.write("<pre>") req.write(calendar.month(2005, int(month),2,3)) req.write("</pre>") else: %> <form method="POST" action="../psp/getmonth.psp"> <p> <hr> <br /> Show which month for 2005?: <br /> <select name="month"> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> <option value="4">April</option> <option value="5">May</option> <option value="6">June</option> <option value="7">July</option> <option value="8">August</option> <option value="9">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> <br /> <input type="submit" value="Show Month"> </form>

The PSP model can be tempting for new programmers—or at least those new to mod_python programming. You can use PSP with other handlers and thus allow some control over how your code gets integrated into larger solutions but that can create a hard-to-manage code base. The PSP model also makes it difficult to create an application where the presentation is cleanly separated from the logic. For more information on this problem you may want to review the Model-View-Controller (MVC) writeup on Wikipedia.

A better way to use PSP is as a template language. You can use standard HTML content and embed it into a PSP file like the one shown earlier. Then post data to a mod_python script on the server that processes the PSP template.

Treating the file as a template, using the same HTML as the Publisher Handler illustration, you can modify the Python code as follows:

#!/usr/bin/python # # # A simple script to output a calendar month based off input # from a web form. # # import calendar from mod_python import apache, psp def getMonth(req,month): template = psp.PSP(req, filename='../psp/getmonth.psp') s = calendar.month(2005, int(month),2,3) req.content_type = "text/html" template.run({'displaymonth' : s })

You then need to create the getmonth.psp file as mentioned in the line containing the template variable. .

<html> <body> <pre> <%=displaymonth%> </pre> </body> </html>

Treating the PSP file as a template allows you to effectively keep logic out of the presentation.

The CGI Handler
The CGI Handler is the mechanism used for the benchmarking at the beginning of this article. It is designed to allow someone who has written a CGI application in Python to migrate to mod_python with minimal changes to the application. If you are migrating from a standard CGI installation, this method of using mod_python is going to feel the most comfortable. However, the CGI handler is not thread safe and will suffer performance problems with busy Web sites compared to the Publisher Handler.

Here's the CGI Handler used to execute the example script.

<Directory /var/www/cgi-bin> SetHandler mod_python PythonHandler mod_python.cgihandler </Directory>

The configuration sets a directory for executing python scripts with the cgihandler. You don't have to specify a cgi-bin directory to enable the CGI Handler; the name was chosen purely for consistency.

It is possible to have more than one handler for your application. During migration you might have one handler that is executing legacy CGI scripts and another that executes your mod_python publisher compatible scripts.

This brief introduction should give you a taste of mod_python's possibilities. Depending on which handler you're using, you can run Python scripts in a manner similar to CGI, treat Python as an embedded page language, or use templates and associated code files to achieve a clean separation between presentation and logic.



Joshua D. Drake is President of Command Prompt, Inc. a dedicated PostgreSQL support and custom programming company. He is also the co-author of "Practical PostgreSQL" from O'Reilly and Associates.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap