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

« back to all changes in this revision

Viewing changes to doc/howto/tutorial/factory.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: using a single factory for
3
 
    multiple protocols</title><link href="../stylesheet.css" type="text/css" rel="stylesheet" /></head><body bgcolor="white"><h1 class="title">The Evolution of Finger: using a single factory for
4
 
    multiple protocols</h1><div class="toc"><ol><li><a href="#auto0">Introduction</a></li><li><a href="#auto1">Support HTTPS</a></li></ol></div><div class="content"><span></span><h2>Introduction<a name="auto0"></a></h2><p> This is the eighth part of the Twisted tutorial <a href="index.html">Twisted from Scratch, or The Evolution of Finger</a>.</p><p>In this part, we add HTTPS support to our web frontend, showing how to have
5
 
a single factory listen on multiple ports.</p><h2>Support HTTPS<a name="auto1"></a></h2><p>All we need to do to code an HTTPS site is just write a context factory (in
 
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: using a single factory for
 
8
    multiple protocols</title>
 
9
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
 
10
  </head>
 
11
 
 
12
  <body bgcolor="white">
 
13
    <h1 class="title">The Evolution of Finger: using a single factory for
 
14
    multiple protocols</h1>
 
15
    <div class="toc"><ol><li><a href="#auto0">Introduction</a></li><li><a href="#auto1">Support HTTPS</a></li></ol></div>
 
16
    <div class="content">
 
17
 
 
18
<span/>
 
19
 
 
20
<h2>Introduction<a name="auto0"/></h2>
 
21
 
 
22
<p> This is the eighth part of the Twisted tutorial <a href="index.html" shape="rect">Twisted from Scratch, or The Evolution of Finger</a>.</p>
 
23
 
 
24
<p>In this part, we add HTTPS support to our web frontend, showing how to have
 
25
a single factory listen on multiple ports.</p>
 
26
 
 
27
<h2>Support HTTPS<a name="auto1"/></h2>
 
28
 
 
29
<p>All we need to do to code an HTTPS site is just write a context factory (in
6
30
this case, which loads the certificate from a certain file) and then use the
7
31
twisted.application.internet.SSLServer method. Note that one factory (in this
8
 
case, a site) can listen on multiple ports with multiple protocols.</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>
 
32
case, a site) can listen on multiple ports with multiple protocols.</p>
 
33
 
 
34
<div class="py-listing"><pre><p class="py-linenumber">  1
 
35
  2
 
36
  3
 
37
  4
 
38
  5
 
39
  6
 
40
  7
 
41
  8
 
42
  9
 
43
 10
 
44
 11
 
45
 12
 
46
 13
 
47
 14
 
48
 15
 
49
 16
 
50
 17
 
51
 18
 
52
 19
 
53
 20
 
54
 21
 
55
 22
 
56
 23
 
57
 24
 
58
 25
 
59
 26
 
60
 27
 
61
 28
 
62
 29
 
63
 30
 
64
 31
 
65
 32
 
66
 33
 
67
 34
 
68
 35
 
69
 36
 
70
 37
 
71
 38
 
72
 39
 
73
 40
 
74
 41
 
75
 42
 
76
 43
 
77
 44
 
78
 45
 
79
 46
 
80
 47
 
81
 48
 
82
 49
 
83
 50
 
84
 51
 
85
 52
 
86
 53
 
87
 54
 
88
 55
 
89
 56
 
90
 57
 
91
 58
 
92
 59
 
93
 60
 
94
 61
 
95
 62
 
96
 63
 
97
 64
 
98
 65
 
99
 66
 
100
 67
 
101
 68
 
102
 69
 
103
 70
 
104
 71
 
105
 72
 
106
 73
 
107
 74
 
108
 75
 
109
 76
 
110
 77
 
111
 78
 
112
 79
 
113
 80
 
114
 81
 
115
 82
 
116
 83
 
117
 84
 
118
 85
 
119
 86
 
120
 87
 
121
 88
 
122
 89
 
123
 90
 
124
 91
 
125
 92
 
126
 93
 
127
 94
 
128
 95
 
129
 96
 
130
 97
 
131
 98
 
132
 99
 
133
100
 
134
101
 
135
102
 
136
103
 
137
104
 
138
105
 
139
106
 
140
107
 
141
108
 
142
109
 
143
110
 
144
111
 
145
112
 
146
113
 
147
114
 
148
115
 
149
116
 
150
117
 
151
118
 
152
119
 
153
120
 
154
121
 
155
122
 
156
123
 
157
124
 
158
125
 
159
126
 
160
127
 
161
128
 
162
129
 
163
130
 
164
131
 
165
132
 
166
133
 
167
134
 
168
135
 
169
136
 
170
137
 
171
138
 
172
139
 
173
140
 
174
141
 
175
142
 
176
143
 
177
144
 
178
145
 
179
146
 
180
147
 
181
148
 
182
149
 
183
150
 
184
151
 
185
152
 
186
153
 
187
154
 
188
155
 
189
156
 
190
157
 
191
158
 
192
159
 
193
160
 
194
161
 
195
162
 
196
163
 
197
164
 
198
165
 
199
166
 
200
167
 
201
168
 
202
169
 
203
170
 
204
171
 
205
172
 
206
173
 
207
174
 
208
175
 
209
176
 
210
177
 
211
178
 
212
179
 
213
180
 
214
181
 
215
182
 
216
183
 
217
184
 
218
185
 
219
186
 
220
187
 
221
188
 
222
189
 
223
190
 
224
191
 
225
192
 
226
193
 
227
194
 
228
195
 
229
196
 
230
197
 
231
198
 
232
199
 
233
200
 
234
201
 
235
202
 
236
203
 
237
204
 
238
205
 
239
206
 
240
207
 
241
208
 
242
209
 
243
210
 
244
211
 
245
212
 
246
213
 
247
214
 
248
215
 
249
216
 
250
217
 
251
218
 
252
219
 
253
220
 
254
221
 
255
222
 
256
223
 
257
224
 
258
225
 
259
226
 
260
227
 
261
228
 
262
229
 
263
230
 
264
231
 
265
232
 
266
233
 
267
234
 
268
235
 
269
236
 
270
237
 
271
238
 
272
239
 
273
240
 
274
241
 
275
242
 
276
243
 
277
244
 
278
245
 
279
246
 
280
247
 
281
248
 
282
249
 
283
250
 
284
251
 
285
252
 
286
253
 
287
254
 
288
255
 
289
256
 
290
257
 
291
258
 
292
259
 
293
260
 
294
261
 
295
262
 
296
263
 
297
264
 
298
265
 
299
266
 
300
267
 
301
268
 
302
269
 
303
270
 
304
271
 
305
272
 
306
273
 
307
274
 
308
275
 
309
276
 
310
277
 
311
278
 
312
279
 
313
280
 
314
281
 
315
282
 
316
283
 
317
284
 
318
285
 
319
286
 
320
287
 
321
288
 
322
289
 
323
290
 
324
291
 
325
292
 
326
293
 
327
294
 
328
295
 
329
296
 
330
297
 
331
</p><span class="py-src-comment"># Do everything properly, and componentize</span>
 
332
<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
333
<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
334
<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
335
<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>
160
482
        <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>)
161
483
        <span class="py-src-variable">self</span>.<span class="py-src-variable">service</span>=<span class="py-src-variable">service</span>
162
484
 
163
 
        <span class="py-src-comment"># add a specific child for the path &quot;RPC2&quot;
164
 
</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>))
 
485
        <span class="py-src-comment"># add a specific child for the path &quot;RPC2&quot;</span>
 
486
        <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>))
165
487
 
166
 
        <span class="py-src-comment"># need to do this for resources at the root of the site
167
 
</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>)
 
488
        <span class="py-src-comment"># need to do this for resources at the root of the site</span>
 
489
        <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>)
168
490
 
169
491
    <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>):
170
492
        <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>)
181
503
        <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>()
182
504
        <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>)
183
505
 
184
 
        <span class="py-src-comment"># signal that the rendering is not complete
185
 
</span>        <span class="py-src-keyword">return</span> <span class="py-src-variable">server</span>.<span class="py-src-variable">NOT_DONE_YET</span>
 
506
        <span class="py-src-comment"># signal that the rendering is not complete</span>
 
507
        <span class="py-src-keyword">return</span> <span class="py-src-variable">server</span>.<span class="py-src-variable">NOT_DONE_YET</span>
186
508
 
187
509
    <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>):
188
510
        <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>)
207
529
        <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>)
208
530
        <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>)
209
531
 
210
 
        <span class="py-src-comment"># signal that the rendering is not complete
211
 
</span>        <span class="py-src-keyword">return</span> <span class="py-src-variable">server</span>.<span class="py-src-variable">NOT_DONE_YET</span>
 
532
        <span class="py-src-comment"># signal that the rendering is not complete</span>
 
533
        <span class="py-src-keyword">return</span> <span class="py-src-variable">server</span>.<span class="py-src-variable">NOT_DONE_YET</span>
212
534
 
213
535
<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>):
214
536
 
255
577
 
256
578
    <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>):
257
579
        <span class="py-src-variable">self</span>.<span class="py-src-variable">filename</span> = <span class="py-src-variable">filename</span>
 
580
        <span class="py-src-variable">self</span>.<span class="py-src-variable">users</span> = {}
258
581
        <span class="py-src-variable">self</span>.<span class="py-src-variable">_read</span>()
259
582
 
260
583
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">_read</span>(<span class="py-src-parameter">self</span>):
261
 
        <span class="py-src-variable">self</span>.<span class="py-src-variable">users</span> = {}
 
584
        <span class="py-src-variable">self</span>.<span class="py-src-variable">users</span>.<span class="py-src-variable">clear</span>()
262
585
        <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>):
263
586
            <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>)
264
587
            <span class="py-src-variable">user</span> = <span class="py-src-variable">user</span>.<span class="py-src-variable">strip</span>()
302
625
                   ).<span class="py-src-variable">setServiceParent</span>(<span class="py-src-variable">serviceCollection</span>)
303
626
<span class="py-src-variable">internet</span>.<span class="py-src-variable">TCPServer</span>(<span class="py-src-number">8889</span>, <span class="py-src-variable">pb</span>.<span class="py-src-variable">PBServerFactory</span>(<span class="py-src-variable">IPerspectiveFinger</span>(<span class="py-src-variable">f</span>))
304
627
                   ).<span class="py-src-variable">setServiceParent</span>(<span class="py-src-variable">serviceCollection</span>)
305
 
</pre><div class="caption">Source listing - <a href="listings/finger/finger22.py"><span class="filename">listings/finger/finger22.py</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'
 
628
</pre><div class="caption">Source listing - <a href="listings/finger/finger22.py"><span class="filename">listings/finger/finger22.py</span></a></div></div>
 
629
 
 
630
 
 
631
</div>
 
632
 
 
633
    <p><a href="../index.html">Index</a></p>
 
634
    <span class="version">Version: 9.0.0</span>
 
635
  </body>
 
636
</html>
 
 
b'\\ No newline at end of file'