[Tutor] hand-holding for web development

Jay Loden python at jayloden.com
Mon Oct 17 21:13:35 CEST 2005


You need an Apache config section to tell it what to use mod_python on and 
how. For example, in httpd.conf or a separate file in your conf.d directory 
for apache: 

<Directory /var/www/html/>
       AddHandler      mod_python .py
       PythonHandler test
       PythonDebug On
</Directory>

tells Apache to use mod_python on all .py files in my /var/www/html directory 
(which is also the document root on my server). 

Once that's loaded you should be able to run whatever your python code through 
a handler. For example, saving this as test.py: 

#
#mod_python super basic test script
#
from mod_python import apache

def handler(req):
   req.content_type = "text/html"
   req_file = basename(req.uri)
   if req.header_only:
      return apache.OK

   else:
      req.write("mod_python is working")
      return apache.OK

# end of script

This should print "mod_python is working" to the browser if you visit 
http://yoursite.com/test.py 

Remember that you need to restart/reload apache after any config change. The 
basic gist is that any .py file you load in the /var/www/html runs the script 
"test.py" (PythonHandler test) and executes the "handler()" function within 
the script. NOTE: this can be very non-intuitive, becuase running 
http://yoursite.com/test.py has exactly the same result as 
http;//yoursite.com/foo.py - because they're both simply running the handler 
script, test.py, for ALL .py files in the directory. That means that your 
PythonHandler script needs to dispatch other scripts or load special pages in 
order to get different code to run. Also note that there are some pre-written 
Handler scripts that come with mod_python that can often do most of your work 
for you.

You'll probably want a good tutorial on using mod_python to make sense of all 
this, because it's not really intuitive and it works differently than 
traditional cgi programming. Take a look at: 
http://modpython.org/live/current/doc-html/ 
to get you started, it's a lot more detailed and will probably answer your 
questions better. 

-Jay

On Thursday 13 October 2005 3:49 pm, nitin chandra wrote:
> Hi!...
> i am new to Python and i want to develop a website with forms; data
> submitted through forms will be stored in PostgreSQL.
> I am working on derivant of FC3, OpenLX.
> I have Apache 2.0.54 version installed, which is pre-configured with
> mod_python.
> how do i load and view my .py/.html web page (file) on the browser.
> i need the initial hand holding/guidance of how to start, configure , and
> develop modules.
>  thank in advance.
>  Nitin Chandra


More information about the Tutor mailing list