Home arrow Python arrow Page 5 - A Brief Look at Mod_Python

Python Server Pages - Python

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.

  1. A Brief Look at Mod_Python
  2. Installing Mod_Python
  3. Getting Started
  4. Creating Handlers and Working with the API
  5. Python Server Pages
By: Peyton McCullough
Rating: starstarstarstarstar / 20
April 25, 2005

print this article



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:

AddHandler mod_python.psp
PythonHandler mod_python.psp

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:


Here an example:

# Import a Module
import math
<title>Python Server Pages Test</title>
The square root of 25 is <%=math.sqrt ( 25 )%>. The square root of 50 is <%=math.sqrt ( 50 )%>.

Save it as test.psp and point your browser to it. Interesting, eh?


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.

>>> More Python Articles          >>> More By Peyton McCullough

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: