[mod_python] getting started

Gregory (Grisha) Trubetskoy grisha at modpython.org
Wed Oct 8 14:10:53 EST 2003


On Wed, 8 Oct 2003, Anthony Barker wrote:

> I was wondering how to get started processing an html form.
>
> 1) is there something equivalent to Apache::PerlRun?
>
> "This module does not cache compiled scripts between runs. A script is
> loaded and compiled freshly each time it is requested. However,
> Apache::PerlRun still avoids the overhead of starting a new Perl
> interpreter for each CGI script, so it's faster than traditional Perl
> CGI scripting but slower than Apache::Registry or vanilla Apache API
> modules."

No. (The cgi handler is as close as you will get to this). I don't know
what the Perl folks do, but on Python side the only way to achieve this
(including a clean namespace) is to reset the interpreter, which only
results in marginal improvement. Not to mention that there is something
awkward with this whole approach :-)

> 2) How would mod_python enable this simple script?
>
> #!/usr/bin/python2
> import cgi, sys, string
> sys.stderr = sys.stdout
> print "Content-type: text/html\r\n"
>
> out1 = """
> <HTML>
> <HEAD><TITLE>Info Form</TITLE></HEAD>
> <BODY BGCOLOR = white>
> <H3>Please, enter your name and age.</H3>
> <TABLE BORDER = 0>
> <FORM METHOD = post ACTION = "t3.cgi">
> <TR><TH>Name:</TH><TD>
> <INPUT type = text name = "name"></TD>
> </TABLE>
> <INPUT TYPE = hidden NAME = "action" VALUE = "display"
> <INPUT TYPE = submit VALUE = "Enter">
> </FORM>
> </BODY>
> </HTML>"""
>
> form = cgi.FieldStorage()
> if form.has_key("action"):
>     if form.has_key("name"):
>         print "hello " + form["name"].value
> else:
>     print out1
>

If you are using the publisher handler, then your Apache config would look
like this (and make sure it is not a CGI ScriptAlias):

AddHandler python-program .py
PythonHandler mod_python.publisher
PythonDebug On

And the script could be something like the below. If the script was named
form.py, you'd access the below via http://blah.bla.com/form.py/info.


def info(name=None, action=None):

    out1 = """
    <HTML>
    <HEAD><TITLE>Info Form</TITLE></HEAD>
    <BODY BGCOLOR = white>
    <H3>Please, enter your name and age.</H3>
    <TABLE BORDER = 0>
    <FORM METHOD = post ACTION = "info">
    <TR><TH>Name:</TH><TD>
    <INPUT type = text name = "name"></TD>
    </TABLE>
    <INPUT TYPE = hidden NAME = "action" VALUE = "display"
    <INPUT TYPE = submit VALUE = "Enter">
    </FORM>
    </BODY>
    </HTML>"""

    if action and name:
        return "hello %s" % name
    else:
        return out1



HTH,

Grisha


More information about the Mod_python mailing list