Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 15533144ea879a3ee7f2cdcbb31e4859 > files > 140

apache-mod_python-doc-3.3.1-12mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="modpython.css" type='text/css' />
<link rel="first" href="modpython.html" title='Mod_python Manual' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="tut-overview.html" />
<link rel="prev" href="tutorial.html" />
<link rel="parent" href="tutorial.html" />
<link rel="next" href="tut-overview.html" />
<meta name='aesop' content='information' />
<title>3.1 A Quick Start with the Publisher Handler</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3. Tutorial"
  href="tutorial.html"><img src='previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="3. Tutorial"
  href="tutorial.html"><img src='up.png'
  border='0' height='32'  alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.2 Quick Overview of"
  href="tut-overview.html"><img src='next.png'
  border='0' height='32'  alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Mod_python Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='contents.png'
  border='0' height='32'  alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='blank.png'
  border='0' height='32'  alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='index.png'
  border='0' height='32'  alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="tutorial.html">3. Tutorial</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="tutorial.html">3. Tutorial</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="tut-overview.html">3.2 Quick Overview of</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION005100000000000000000"></A><A NAME="tut-pub"></A>
<BR>
3.1 A Quick Start with the Publisher Handler
</H1>

<P>
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.

<P>
The <code>publisher</code> 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:

<P>
<div class="verbatim"><pre>
  AddHandler mod_python .py
  PythonHandler mod_python.publisher
  PythonDebug On
</pre></div>

<P>
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:
<span class="file">form.html</span> - the form to collect the data, and
<span class="file">form.py</span> - the target of the form's action.

<P>
Here is the html for the form:

<P>
<div class="verbatim"><pre>
  &lt;html&gt;
      Please provide feedback below:
  &lt;p&gt;                           
  &lt;form action="form.py/email" method="POST"&gt;

      Name:    &lt;input type="text" name="name"&gt;&lt;br&gt;
      Email:   &lt;input type="text" name="email"&gt;&lt;br&gt;
      Comment: &lt;textarea name="comment" rows=4 cols=20&gt;&lt;/textarea&gt;&lt;br&gt;
      &lt;input type="submit"&gt;

  &lt;/form&gt;
  &lt;/html&gt;
</pre></div>

<P>
Note the <code>action</code> element of the <code>&lt;form&gt;</code> tag points to
<code>form.py/email</code>. We are going to create a file called
<span class="file">form.py</span>, like this:

<P>
<div class="verbatim"><pre>
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 = """\
&lt;html&gt;

Dear %s,&lt;br&gt;                                                                                                                                       
Thank You for your kind comments, we
will get back to you shortly.

&lt;/html&gt;""" % name

    return s
</pre></div>

<P>
When the user clicks the Submit button, the publisher handler will
load the <tt class="function">email</tt> function in the <tt class="module">form</tt> module,
passing it the form fields as keyword arguments. It will also pass the
request object as <code>req</code>.

<P>
Note that you do not have to have <code>req</code> 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.

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

<P>
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 <code>req.headers_out</code>, return errors by raising
<tt class="exception">apache.SERVER_ERROR</tt> exceptions, write or read directly to
and from the client via <tt class="method">req.write()</tt> and <tt class="method">req.read()</tt>,
etc.

<P>
Read Section <A href="hand-pub.html#hand-pub">7.1</A> <em class="citetitle"><a
 href="hand-pub.html"
 title="Publisher Handler"
 >Publisher Handler</a></em>
for more information on the publisher handler. 

<P>

<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3. Tutorial"
  href="tutorial.html"><img src='previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="3. Tutorial"
  href="tutorial.html"><img src='up.png'
  border='0' height='32'  alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.2 Quick Overview of"
  href="tut-overview.html"><img src='next.png'
  border='0' height='32'  alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Mod_python Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='contents.png'
  border='0' height='32'  alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='blank.png'
  border='0' height='32'  alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='index.png'
  border='0' height='32'  alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="tutorial.html">3. Tutorial</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="tutorial.html">3. Tutorial</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="tut-overview.html">3.2 Quick Overview of</A>
</div>
</div>
<hr />
<span class="release-info">Release 3.3.1, documentation updated on January 29, 2007.</span>
</DIV>
<!--End of Navigation Panel-->

</BODY>
</HTML>