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

Getting Started - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

http://server/pypublish/test.py

As you can see, the index function is executed, and the results are delivered to you. Now let's try something different. Append "/peyton" to the URL:

http://server/pypublish/test.py/peyton

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?



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- 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: