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.
Now I'll introduce Python Server Pages. Languages like PHP have been made popular due to their ability to be embedded directly into HTML. Python Server Pages allow Python code to be embedded into HTML pages. Python Server Pages aren't terribly difficult to understand, and I believe showing you would be a lot better than telling you, so we'll jump right in. Create a directory named pypsp, and add this to its .htaccess file:
Now let's get into some code. Python code is enclosed between less-than and greater-than signs, postfixed and prefixed with percent signs. For example:
<% # Python code here %>
You can also print out single items by putting an equal sign in, just as you would with PHP:
<%=variableName%>
Here an example:
<% # Import a Module import math %> <html> <head> <title>Python Server Pages Test</title> </head> <body> The square root of 25 is <%=math.sqrt ( 25 )%>. The square root of 50 is <%=math.sqrt ( 50 )%>. </body> </html>
Save it as test.psp and point your browser to it. Interesting, eh?
Conclusion
As you've seen throughout this article, mod_python is a very powerful addition to the Apache Web server. It allows control over even Apache itself through handlers, which are easily created with mod_python. The publisher handle and the psp handle make Web development in Python a lot easier than it already is, and allow it to better compete with popular Web development languages such as PHP and ASP.
What this article outlines is definitely not all of mod_python. Mod_python contains much more power than this article can tell about, but that power is the topic of another article on another day. Until then, good luck.