SunQuest
 
       Python
  Home arrow Python arrow Page 3 - A Brief Look at Mod_Python
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

A Brief Look at Mod_Python
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 19
    2005-04-25

    Table of Contents:
  • A Brief Look at Mod_Python
  • Installing Mod_Python
  • Getting Started
  • Creating Handlers and Working with the API
  • Python Server Pages

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    IBM developerWorks
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    A Brief Look at Mod_Python - Getting Started


    (Page 3 of 5 )

    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


       · Python source examples are wrongly formatted and so syntactically incorrect. Please...
       · The examples won't work, since the indentation has been lost. Hopefully the author...
       · We have changed the code samples to the proper indentation that the author provided....
       · I installed mod_python in my apache server but apache is not starting now. When I...
       · Nice Intro - thanx!The following example needed a <html> tag, due to Mime...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway