~certify-web-dev/twisted/certify-staging

« back to all changes in this revision

Viewing changes to doc/howto/tutorial/web.html

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-01-02 19:38:17 UTC
  • mfrom: (2.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100102193817-jphp464ppwh7dulg
Tags: 9.0.0-1
* python-twisted: Depend on the python-twisted-* 9.0 packages.
* python-twisted: Depend on python-zope.interface only. Closes: #557781.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?xml version="1.0"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><title>Twisted Documentation: The Evolution of Finger: a web frontend</title><link href="../stylesheet.css" type="text/css" rel="stylesheet" /></head><body bgcolor="white"><h1 class="title">The Evolution of Finger: a web frontend</h1><div class="toc"><ol><li><a href="#auto0">Introduction</a></li></ol></div><div class="content"><span></span><h2>Introduction<a name="auto0"></a></h2><p> This is the sixth part of the Twisted tutorial <a href="index.html">Twisted from Scratch, or The Evolution of Finger</a>.</p><p>In this part, we demonstrate adding a web frontend using simple <code class="API"><a href="http://twistedmatrix.com/documents/8.2.0/api/twisted.web.resource.Resource.html" title="twisted.web.resource.Resource">twisted.web.resource.Resource</a></code> objects: <code class="python">UserStatusTree</code>, which will produce a listing of all
 
1
<?xml version="1.0" encoding="utf-8"?>
 
2
<!DOCTYPE html
 
3
  PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
 
4
  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
 
5
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
 
6
  <head>
 
7
<title>Twisted Documentation: The Evolution of Finger: a web frontend</title>
 
8
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
 
9
  </head>
 
10
 
 
11
  <body bgcolor="white">
 
12
    <h1 class="title">The Evolution of Finger: a web frontend</h1>
 
13
    <div class="toc"><ol><li><a href="#auto0">Introduction</a></li></ol></div>
 
14
    <div class="content">
 
15
 
 
16
<span/>
 
17
 
 
18
<h2>Introduction<a name="auto0"/></h2>
 
19
 
 
20
<p> This is the sixth part of the Twisted tutorial <a href="index.html" shape="rect">Twisted from Scratch, or The Evolution of Finger</a>.</p>
 
21
 
 
22
<p>In this part, we demonstrate adding a web frontend using simple <code class="API"><a href="http://twistedmatrix.com/documents/9.0.0/api/twisted.web.resource.Resource.html" title="twisted.web.resource.Resource">twisted.web.resource.Resource</a></code> objects: <code class="python">UserStatusTree</code>, which will produce a listing of all
3
23
users at the base URL (<code>/</code>) of our site; <code class="python">UserStatus</code>, which gives the status of each user at the
4
24
locaton <code>/username</code>; and <code class="python">UserStatusXR</code>,
5
25
which exposes an XMLRPC interface to <code class="python">getUser</code> and
6
 
<code class="python">getUsers</code> functions at the URL <code>/RPC2</code>.</p><p>In this example we construct HTML segments manually. If the web interface
 
26
<code class="python">getUsers</code> functions at the URL <code>/RPC2</code>.</p>
 
27
 
 
28
<p>In this example we construct HTML segments manually. If the web interface
7
29
was less trivial, we would want to use more sophisticated web templating and
8
 
design our system so that HTML rendering and logic were clearly separated.</p><div class="py-listing"><pre>
9
 
<span class="py-src-comment"># Do everything properly, and componentize
10
 
</span><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">application</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">internet</span>, <span class="py-src-variable">service</span>
 
30
design our system so that HTML rendering and logic were clearly separated.</p>
 
31
 
 
32
<div class="py-listing"><pre><p class="py-linenumber">  1
 
33
  2
 
34
  3
 
35
  4
 
36
  5
 
37
  6
 
38
  7
 
39
  8
 
40
  9
 
41
 10
 
42
 11
 
43
 12
 
44
 13
 
45
 14
 
46
 15
 
47
 16
 
48
 17
 
49
 18
 
50
 19
 
51
 20
 
52
 21
 
53
 22
 
54
 23
 
55
 24
 
56
 25
 
57
 26
 
58
 27
 
59
 28
 
60
 29
 
61
 30
 
62
 31
 
63
 32
 
64
 33
 
65
 34
 
66
 35
 
67
 36
 
68
 37
 
69
 38
 
70
 39
 
71
 40
 
72
 41
 
73
 42
 
74
 43
 
75
 44
 
76
 45
 
77
 46
 
78
 47
 
79
 48
 
80
 49
 
81
 50
 
82
 51
 
83
 52
 
84
 53
 
85
 54
 
86
 55
 
87
 56
 
88
 57
 
89
 58
 
90
 59
 
91
 60
 
92
 61
 
93
 62
 
94
 63
 
95
 64
 
96
 65
 
97
 66
 
98
 67
 
99
 68
 
100
 69
 
101
 70
 
102
 71
 
103
 72
 
104
 73
 
105
 74
 
106
 75
 
107
 76
 
108
 77
 
109
 78
 
110
 79
 
111
 80
 
112
 81
 
113
 82
 
114
 83
 
115
 84
 
116
 85
 
117
 86
 
118
 87
 
119
 88
 
120
 89
 
121
 90
 
122
 91
 
123
 92
 
124
 93
 
125
 94
 
126
 95
 
127
 96
 
128
 97
 
129
 98
 
130
 99
 
131
100
 
132
101
 
133
102
 
134
103
 
135
104
 
136
105
 
137
106
 
138
107
 
139
108
 
140
109
 
141
110
 
142
111
 
143
112
 
144
113
 
145
114
 
146
115
 
147
116
 
148
117
 
149
118
 
150
119
 
151
120
 
152
121
 
153
122
 
154
123
 
155
124
 
156
125
 
157
126
 
158
127
 
159
128
 
160
129
 
161
130
 
162
131
 
163
132
 
164
133
 
165
134
 
166
135
 
167
136
 
168
137
 
169
138
 
170
139
 
171
140
 
172
141
 
173
142
 
174
143
 
175
144
 
176
145
 
177
146
 
178
147
 
179
148
 
180
149
 
181
150
 
182
151
 
183
152
 
184
153
 
185
154
 
186
155
 
187
156
 
188
157
 
189
158
 
190
159
 
191
160
 
192
161
 
193
162
 
194
163
 
195
164
 
196
165
 
197
166
 
198
167
 
199
168
 
200
169
 
201
170
 
202
171
 
203
172
 
204
173
 
205
174
 
206
175
 
207
176
 
208
177
 
209
178
 
210
179
 
211
180
 
212
181
 
213
182
 
214
183
 
215
184
 
216
185
 
217
186
 
218
187
 
219
188
 
220
189
 
221
190
 
222
191
 
223
192
 
224
193
 
225
194
 
226
195
 
227
196
 
228
197
 
229
198
 
230
199
 
231
200
 
232
201
 
233
202
 
234
203
 
235
204
 
236
205
 
237
206
 
238
207
 
239
208
 
240
209
 
241
210
 
242
211
 
243
212
 
244
213
 
245
214
 
246
215
 
247
216
 
248
217
 
249
218
 
250
219
 
251
220
 
252
221
 
253
222
 
254
223
 
255
224
 
256
225
 
257
226
 
258
227
 
259
228
 
260
229
 
261
230
 
262
231
 
263
232
 
264
233
 
265
234
 
266
235
 
267
236
 
268
237
 
269
238
 
270
239
 
271
240
 
272
241
 
273
242
 
274
243
 
275
244
 
276
245
 
277
246
 
278
247
 
279
248
 
280
249
 
281
250
 
282
251
 
283
</p><span class="py-src-comment"># Do everything properly, and componentize</span>
 
284
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">application</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">internet</span>, <span class="py-src-variable">service</span>
11
285
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">protocol</span>, <span class="py-src-variable">reactor</span>, <span class="py-src-variable">defer</span>
12
286
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">words</span>.<span class="py-src-variable">protocols</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">irc</span>
13
287
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">protocols</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">basic</span>
158
432
        <span class="py-src-variable">resource</span>.<span class="py-src-variable">Resource</span>.<span class="py-src-variable">__init__</span>(<span class="py-src-variable">self</span>)
159
433
        <span class="py-src-variable">self</span>.<span class="py-src-variable">service</span>=<span class="py-src-variable">service</span>
160
434
 
161
 
        <span class="py-src-comment"># add a specific child for the path &quot;RPC2&quot;
162
 
</span>        <span class="py-src-variable">self</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;RPC2&quot;</span>, <span class="py-src-variable">UserStatusXR</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">service</span>))
 
435
        <span class="py-src-comment"># add a specific child for the path &quot;RPC2&quot;</span>
 
436
        <span class="py-src-variable">self</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;RPC2&quot;</span>, <span class="py-src-variable">UserStatusXR</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">service</span>))
163
437
 
164
 
        <span class="py-src-comment"># need to do this for resources at the root of the site
165
 
</span>        <span class="py-src-variable">self</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;&quot;</span>, <span class="py-src-variable">self</span>)
 
438
        <span class="py-src-comment"># need to do this for resources at the root of the site</span>
 
439
        <span class="py-src-variable">self</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;&quot;</span>, <span class="py-src-variable">self</span>)
166
440
 
167
441
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">_cb_render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">users</span>, <span class="py-src-parameter">request</span>):
168
442
        <span class="py-src-variable">userOutput</span> = <span class="py-src-string">''</span>.<span class="py-src-variable">join</span>([<span class="py-src-string">&quot;&lt;li&gt;&lt;a href=\&quot;%s\&quot;&gt;%s&lt;/a&gt;&lt;/li&gt;&quot;</span> % (<span class="py-src-variable">user</span>, <span class="py-src-variable">user</span>)
179
453
        <span class="py-src-variable">d</span> = <span class="py-src-variable">self</span>.<span class="py-src-variable">service</span>.<span class="py-src-variable">getUsers</span>()
180
454
        <span class="py-src-variable">d</span>.<span class="py-src-variable">addCallback</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">_cb_render_GET</span>, <span class="py-src-variable">request</span>)
181
455
 
182
 
        <span class="py-src-comment"># signal that the rendering is not complete
183
 
</span>        <span class="py-src-keyword">return</span> <span class="py-src-variable">server</span>.<span class="py-src-variable">NOT_DONE_YET</span>
 
456
        <span class="py-src-comment"># signal that the rendering is not complete</span>
 
457
        <span class="py-src-keyword">return</span> <span class="py-src-variable">server</span>.<span class="py-src-variable">NOT_DONE_YET</span>
184
458
 
185
459
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">getChild</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">path</span>, <span class="py-src-parameter">request</span>):
186
460
        <span class="py-src-keyword">return</span> <span class="py-src-variable">UserStatus</span>(<span class="py-src-variable">user</span>=<span class="py-src-variable">path</span>, <span class="py-src-variable">service</span>=<span class="py-src-variable">self</span>.<span class="py-src-variable">service</span>)
205
479
        <span class="py-src-variable">d</span> = <span class="py-src-variable">self</span>.<span class="py-src-variable">service</span>.<span class="py-src-variable">getUser</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">user</span>)
206
480
        <span class="py-src-variable">d</span>.<span class="py-src-variable">addCallback</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">_cb_render_GET</span>, <span class="py-src-variable">request</span>)
207
481
 
208
 
        <span class="py-src-comment"># signal that the rendering is not complete
209
 
</span>        <span class="py-src-keyword">return</span> <span class="py-src-variable">server</span>.<span class="py-src-variable">NOT_DONE_YET</span>
 
482
        <span class="py-src-comment"># signal that the rendering is not complete</span>
 
483
        <span class="py-src-keyword">return</span> <span class="py-src-variable">server</span>.<span class="py-src-variable">NOT_DONE_YET</span>
210
484
 
211
485
<span class="py-src-keyword">class</span> <span class="py-src-identifier">UserStatusXR</span>(<span class="py-src-parameter">xmlrpc</span>.<span class="py-src-parameter">XMLRPC</span>):
212
486
 
227
501
 
228
502
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">filename</span>):
229
503
        <span class="py-src-variable">self</span>.<span class="py-src-variable">filename</span> = <span class="py-src-variable">filename</span>
 
504
        <span class="py-src-variable">self</span>.<span class="py-src-variable">users</span> = {}
230
505
        <span class="py-src-variable">self</span>.<span class="py-src-variable">_read</span>()
231
506
 
232
507
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">_read</span>(<span class="py-src-parameter">self</span>):
233
 
        <span class="py-src-variable">self</span>.<span class="py-src-variable">users</span> = {}
 
508
        <span class="py-src-variable">self</span>.<span class="py-src-variable">users</span>.<span class="py-src-variable">clear</span>()
234
509
        <span class="py-src-keyword">for</span> <span class="py-src-variable">line</span> <span class="py-src-keyword">in</span> <span class="py-src-variable">file</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">filename</span>):
235
510
            <span class="py-src-variable">user</span>, <span class="py-src-variable">status</span> = <span class="py-src-variable">line</span>.<span class="py-src-variable">split</span>(<span class="py-src-string">':'</span>, <span class="py-src-number">1</span>)
236
511
            <span class="py-src-variable">user</span> = <span class="py-src-variable">user</span>.<span class="py-src-variable">strip</span>()
256
531
<span class="py-src-variable">i</span>.<span class="py-src-variable">nickname</span> = <span class="py-src-string">'fingerbot'</span>
257
532
<span class="py-src-variable">internet</span>.<span class="py-src-variable">TCPClient</span>(<span class="py-src-string">'irc.freenode.org'</span>, <span class="py-src-number">6667</span>, <span class="py-src-variable">i</span>
258
533
                   ).<span class="py-src-variable">setServiceParent</span>(<span class="py-src-variable">serviceCollection</span>)
259
 
</pre><div class="caption">Source listing - <a href="listings/finger/finger20.tac"><span class="filename">listings/finger/finger20.tac</span></a></div></div></div><p><a href="../index.html">Index</a></p><span class="version">Version: 8.2.0</span></body></html>
 
 
b'\\ No newline at end of file'
 
534
</pre><div class="caption">Source listing - <a href="listings/finger/finger20.tac"><span class="filename">listings/finger/finger20.tac</span></a></div></div>
 
535
 
 
536
</div>
 
537
 
 
538
    <p><a href="../index.html">Index</a></p>
 
539
    <span class="version">Version: 9.0.0</span>
 
540
  </body>
 
541
</html>
 
 
b'\\ No newline at end of file'