File: tut-pub.html

package info (click to toggle)
libapache-mod-python 2%3A2.7.11-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,328 kB
  • ctags: 848
  • sloc: ansic: 2,785; python: 1,115; sh: 299; makefile: 285
file content (208 lines) | stat: -rw-r--r-- 6,795 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>3.4 Publisher Handler Makes it Easy</title>
<META NAME="description" CONTENT="3.4 Publisher Handler Makes it Easy">
<META NAME="keywords" CONTENT="modpython">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<link rel="STYLESHEET" href="modpython.css">
<link rel="first" href="modpython.html">
<link rel="contents" href="contents.html" title="Contents">
<link rel="index" href="genindex.html" title="Index">
<LINK REL="previous" href="tut-more-complicated.html">
<LINK REL="up" href="tutorial.html">
<LINK REL="next" href="pythonapi.html">
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="tut-more-complicated.html"><img src="icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="tutorial.html"><img src="icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="pythonapi.html"><img src="icons/next.gif"
  border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Mod_python Manual</td>
<td><A href="contents.html"><img src="icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><img src="icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
<td><A href="genindex.html"><img src="icons/index.gif"
  border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="tut-more-complicated.html">3.3 Now something More</A>
<b class="navlabel">Up:</b> <a class="sectref" href="tutorial.html">3. Tutorial</A>
<b class="navlabel">Next:</b> <a class="sectref" href="pythonapi.html">4. Python API</A>
<br><hr>
</DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION005400000000000000000">&nbsp;</A>
<BR>
3.4 Publisher Handler Makes it Easy
</H1>

<P>
At this point you may be wondering if mod_python is all that useful
after all. You may find yourself asking: "If there can only be one
handler per directory, how am I to structure my application?"

<P>
Enter the <code>publisher</code> handler 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>
<dl><dd><pre class="verbatim">
AddHandler python-program .py
PythonHandler mod_python.publisher
</pre></dl>

<P>
The following example will demonstrate a simple feedback form. The
form will ask for the name, e-mail address and a comment and the
will construct an e-mail to the webmaster using the information
submitted by the user.

<P>
Here is the html for the form:

<P>
<dl><dd><pre class="verbatim">
&lt;html&gt;
  Please provide feedback below:
  &lt;p&gt;                           
  &lt;form action="form/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></dl>

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

<P>
<dl><dd><pre class="verbatim">
import smtplib

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

    # see if 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: webmaster

I have the following comment:

%s

Thank You,

%s

""" % (email, comment, name)

    # send it out
    conn = smtplib.SMTP("localhost")
    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></dl>

<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. Note that it will
also pass the <tt class="class">Request</tt> object as <code>req</code>.  Note also 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>
Also notice how it sends data back to the customer - via the return
value of the function. 

<P>
And last, but not the least, note how all the power of mod_python
is still available to this function, since it has access to the
<tt class="class">Request</tt> 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.

<DIV CLASS="navigation">
<p><hr>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="tut-more-complicated.html"><img src="icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="tutorial.html"><img src="icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="pythonapi.html"><img src="icons/next.gif"
  border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Mod_python Manual</td>
<td><A href="contents.html"><img src="icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><img src="icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
<td><A href="genindex.html"><img src="icons/index.gif"
  border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="tut-more-complicated.html">3.3 Now something More</A>
<b class="navlabel">Up:</b> <a class="sectref" href="tutorial.html">3. Tutorial</A>
<b class="navlabel">Next:</b> <a class="sectref" href="pythonapi.html">4. Python API</A>
<hr>
<span class="release-info">Release 2.7.10, documentation updated on December 07, 2003.</span>
</DIV>
<!--End of Navigation Panel-->

</BODY>
</HTML>