3.1 A Quick Start with the Publisher Handler

This section provides a quick overview of the Publisher handler for those who would like to get started without getting into too much detail. A more thorough explanation of how mod_python handlers work and what a handler actually is follows on in the later sections of the tutorial.

The publisher handler is provided as one of the standard mod_python handlers. To get the publisher handler working, you will need the following lines in your config:

  AddHandler mod_python .py
  PythonHandler mod_python.publisher
  PythonDebug On

The following example will demonstrate a simple feedback form. The form will ask for the name, e-mail address and a comment and construct an e-mail to the webmaster using the information submitted by the user. This simple application consists of two files: form.html - the form to collect the data, and form.py - the target of the form's action.

Here is the html for the form:

  <html>
      Please provide feedback below:
  <p>                           
  <form action="form.py/email" method="POST">

      Name:    <input type="text" name="name"><br>
      Email:   <input type="text" name="email"><br>
      Comment: <textarea name="comment" rows=4 cols=20></textarea><br>
      <input type="submit">

  </form>
  </html>

Note the action element of the <form> tag points to form.py/email. We are going to create a file called form.py, like this:

import smtplib

WEBMASTER = "webmaster"   # webmaster e-mail
SMTP_SERVER = "localhost" # your SMTP server

def email(req, name, email, comment):

    # make sure the user provided all the parameters
    if not (name and email and comment):
        return "A required parameter is missing, \
               please go back and correct the error"

    # create the message text
    msg = """\
From: %s                                                                                                                                           
Subject: feedback
To: %s

I have the following comment:

%s

Thank You,

%s

""" % (email, WEBMASTER, comment, name)

    # send it out
    conn = smtplib.SMTP(SMTP_SERVER)
    conn.sendmail(email, [WEBMASTER], msg)
    conn.quit()

    # provide feedback to the user
    s = """\
<html>

Dear %s,<br>                                                                                                                                       
Thank You for your kind comments, we
will get back to you shortly.

</html>""" % name

    return s

When the user clicks the Submit button, the publisher handler will load the email function in the form module, passing it the form fields as keyword arguments. It will also pass the request object as req.

Note that you do not have to have req as one of the arguments if you do not need it. The publisher handler is smart enough to pass your function only those arguments that it will accept.

The data is sent back to the browser via the return value of the function.

Even though the Publisher handler simplifies mod_python programming a great deal, all the power of mod_python is still available to this program, since it has access to the request object. You can do all the same things you can do with a ``native'' mod_python handler, e.g. set custom headers via req.headers_out, return errors by raising apache.SERVER_ERROR exceptions, write or read directly to and from the client via req.write() and req.read(), etc.

Read Section 6.1 Publisher Handler for more information on the publisher handler.