[Tutor] Leaving PHP for Python

bob gailer bgailer at gmail.com
Tue Nov 25 23:44:58 CET 2008


Please always reply all so a copy goes to the tutor list. We all 
participate and learn.

Jason DeBord wrote:
> Bob,
>
> Thanks for the reply.
>
> When I said I don't understand the above, that was just to give you 
> guys a reference for where I am at.
>
> At my current job I rarely touch the web server. I have just been 
> using php for server side development, thus, installing python and 
> mod_python was new territory for me, though I managed to get it 
> working surprisingly quick. As we speak I am trying to get Django up 
> and running. This is proving difficult and I think I am getting ahead 
> of myself.
>
> I'm lost with the code below because up to this point all I have ever 
> had to do is type in " <?php " and away I go.
>
OK - line - by - line commentary:

mod_python is an apache add-on that creates a "long-running" Python 
process. This means the Python interpreter is load once (maximizing 
response time) and there is memory from one invocation by a request to 
the next.

> from mod_python import apache

Here mod_python is a python module that cooperates with the add-on. 
import loads the module (once) and brings the object apache from the 
module namespace into the local namespace.  The reference to apache.OK 
is resolved by looking at the imported object for a property (attribute) 
named OK.
>
> def handler(req):
Defines a function handler. Apache by default calls handler for each 
request, and passes the request object (here named req - arbitrarily).
>    req.write("Hello World!")
calls the req object's write method passing a character string. The 
write method adds the string to the response under construction.
>    return apache.OK
Terminates the function and returns apache.OK (whatever that is).

I believe that returning apache.OK from the handler call tells apache 
that the request is complete and to return the page to the browser.

-- 
Bob Gailer
Chapel Hill NC 
919-636-4239



More information about the Tutor mailing list