~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/web/howto/using-twistedweb.html

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html  PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
 
2
  <head>
 
3
<title>Twisted Documentation: Configuring and Using the Twisted.Web Server</title>
 
4
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
 
5
  </head>
 
6
 
 
7
  <body bgcolor="white">
 
8
    <h1 class="title">Configuring and Using the Twisted.Web Server</h1>
 
9
    <div class="toc"><ol><li><a href="#auto0">Twisted Web Development</a></li><ul><li><a href="#auto1">Main Concepts</a></li><li><a href="#auto2">Site Objects</a></li><li><a href="#auto3">Resource objects</a></li><li><a href="#auto4">Resource Trees</a></li><li><a href="#auto5">.rpy scripts</a></li><li><a href="#auto6">Resource rendering</a></li><li><a href="#auto7">Session</a></li></ul><li><a href="#auto8">Advanced Configuration</a></li><ul><li><a href="#auto9">Adding Children</a></li><li><a href="#auto10">Modifying File Resources</a></li><li><a href="#auto11">Virtual Hosts</a></li><li><a href="#auto12">Advanced Techniques</a></li></ul><li><a href="#auto13">Running a Twisted Web Server</a></li><ul><li><a href="#auto14">Serving Flat HTML</a></li><li><a href="#auto15">Resource Scripts</a></li><li><a href="#auto16">Web UIs</a></li><li><a href="#auto17">Spreadable Web Servers</a></li><li><a href="#auto18">Serving PHP/Perl/CGI</a></li><li><a href="#auto19">Serving WSGI Applications</a></li><li><a href="#auto20">Using VHostMonster</a></li></ul><li><a href="#auto21">Rewriting URLs</a></li><li><a href="#auto22">Knowing When We're Not Wanted</a></li><li><a href="#auto23">As-Is Serving</a></li></ol></div>
 
10
    <div class="content">
 
11
<span/>
 
12
 
 
13
<h2>Twisted Web Development<a name="auto0"/></h2><a name="development" shape="rect"/>
 
14
 
 
15
<p>Twisted Web serves Python objects that implement the interface
 
16
IResource.</p>
 
17
 
 
18
<br clear="none"/><img alt="Twisted Web process" src="../img/web-process.png"/>
 
19
 
 
20
<h3>Main Concepts<a name="auto1"/></h3>
 
21
 
 
22
<ul>
 
23
 
 
24
<li><a href="#sites" shape="rect">Site Objects</a> are responsible for creating
 
25
<code>HTTPChannel</code> instances to parse the HTTP request, and begin the object lookup process. They contain the root Resource, the resource which represents the URL <code>/</code> on the site.</li>
 
26
 
 
27
<li><a href="#resources" shape="rect">Resource</a> objects represent a single URL segment. The <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.resource.IResource.html" title="twisted.web.resource.IResource">IResource</a></code> interface describes the methods a Resource object must implement in order to participate in the object publishing process.</li>
 
28
 
 
29
<li><a href="#trees" shape="rect">Resource trees</a> are arrangements of Resource objects into a Resource tree. Starting at the root Resource object, the tree of Resource objects defines the URLs which will be valid.</li>
 
30
 
 
31
<li><a href="#rpys" shape="rect">.rpy scripts</a> are python scripts which the twisted.web static file server will execute, much like a CGI. However, unlike CGI they must create a Resource object which will be rendered when the URL is visited.</li>
 
32
 
 
33
<li><a href="#rendering" shape="rect">Resource rendering</a> occurs when Twisted Web locates a leaf Resource object. A Resource can either return an html string or write to the request object.</li>
 
34
 
 
35
<li><a href="#sessions" shape="rect">Session</a> objects allow you to store information across multiple requests. Each individual browser using the system has a unique Session instance.</li>
 
36
 
 
37
</ul>
 
38
 
 
39
<p>The Twisted.Web server is started through the Twisted Daemonizer, as in:</p>
 
40
 
 
41
<pre class="shell" xml:space="preserve">
 
42
% twistd web
 
43
</pre>
 
44
 
 
45
<h3>Site Objects<a name="auto2"/></h3>
 
46
<a name="sites" shape="rect"/>
 
47
 
 
48
<p>Site objects serve as the glue between a port to listen for HTTP requests on, and a root Resource object.</p>
 
49
 
 
50
<p>When using <code>twistd -n web --path /foo/bar/baz</code>, a Site object is created with a root Resource that serves files out of the given path.</p>
 
51
 
 
52
<p>You can also create a <code>Site</code> instance by hand, passing it a
 
53
<code>Resource</code> object which will serve as the root of the site:</p>
 
54
 
 
55
<pre class="python"><p class="py-linenumber"> 1
 
56
 2
 
57
 3
 
58
 4
 
59
 5
 
60
 6
 
61
 7
 
62
 8
 
63
 9
 
64
10
 
65
11
 
66
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">server</span>, <span class="py-src-variable">resource</span>
 
67
<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">reactor</span>
 
68
 
 
69
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Simple</span>(<span class="py-src-parameter">resource</span>.<span class="py-src-parameter">Resource</span>):
 
70
    <span class="py-src-variable">isLeaf</span> = <span class="py-src-variable">True</span>
 
71
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
 
72
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;&lt;html&gt;Hello, world!&lt;/html&gt;&quot;</span>
 
73
 
 
74
<span class="py-src-variable">site</span> = <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">Simple</span>())
 
75
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">8080</span>, <span class="py-src-variable">site</span>)
 
76
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
 
77
</pre>
 
78
 
 
79
<h3>Resource objects<a name="auto3"/></h3>
 
80
<a name="resources" shape="rect"/>
 
81
 
 
82
<p><code>Resource</code> objects represent a single URL segment of a site. During URL parsing, <code>getChild</code> is called on the current <code>Resource</code> to produce the next <code>Resource</code> object.</p>
 
83
 
 
84
<p>When the leaf Resource is reached, either because there were no more URL segments or a Resource had isLeaf set to True, the leaf Resource is rendered by calling <code>render(request)</code>. See <q>Resource Rendering</q> below for more about this.</p>
 
85
 
 
86
<p>During the Resource location process, the URL segments which have already been processed and those which have not yet been processed are available in <code>request.prepath</code> and <code>request.postpath</code>.</p>
 
87
 
 
88
<p>A Resource can know where it is in the URL tree by looking at <code>request.prepath</code>, a list of URL segment strings.</p>
 
89
 
 
90
<p>A Resource can know which path segments will be processed after it by looking at <code>request.postpath</code>.</p>
 
91
 
 
92
<p>If the URL ends in a slash, for example <code>http://example.com/foo/bar/</code>, the final URL segment will be an empty string. Resources can thus know if they were requested with or without a final slash.</p>
 
93
 
 
94
<p>Here is a simple Resource object:</p>
 
95
 
 
96
<pre class="python"><p class="py-linenumber"> 1
 
97
 2
 
98
 3
 
99
 4
 
100
 5
 
101
 6
 
102
 7
 
103
 8
 
104
 9
 
105
10
 
106
11
 
107
12
 
108
13
 
109
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">resource</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Resource</span>
 
110
 
 
111
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Hello</span>(<span class="py-src-parameter">Resource</span>):
 
112
    <span class="py-src-variable">isLeaf</span> = <span class="py-src-variable">True</span>
 
113
    <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">name</span>, <span class="py-src-parameter">request</span>):
 
114
        <span class="py-src-keyword">if</span> <span class="py-src-variable">name</span> == <span class="py-src-string">''</span>:
 
115
            <span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>
 
116
        <span class="py-src-keyword">return</span> <span class="py-src-variable">Resource</span>.<span class="py-src-variable">getChild</span>(<span class="py-src-variable">self</span>, <span class="py-src-variable">name</span>, <span class="py-src-variable">request</span>)
 
117
 
 
118
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
 
119
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;Hello, world! I am located at %r.&quot;</span> % (<span class="py-src-variable">request</span>.<span class="py-src-variable">prepath</span>,)
 
120
 
 
121
<span class="py-src-variable">resource</span> = <span class="py-src-variable">Hello</span>()
 
122
</pre>
 
123
 
 
124
<h3>Resource Trees<a name="auto4"/></h3>
 
125
<a name="trees" shape="rect"/>
 
126
 
 
127
<p>Resources can be arranged in trees using <code>putChild</code>. <code>putChild</code> puts a Resource instance into another Resource instance, making it available at the given path segment name:</p>
 
128
 
 
129
<pre class="python"><p class="py-linenumber">1
 
130
2
 
131
3
 
132
</p><span class="py-src-variable">root</span> = <span class="py-src-variable">Hello</span>()
 
133
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">'fred'</span>, <span class="py-src-variable">Hello</span>())
 
134
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">'bob'</span>, <span class="py-src-variable">Hello</span>())
 
135
</pre>
 
136
 
 
137
<p>If this root resource is served as the root of a Site instance, the following URLs will all be valid:</p>
 
138
 
 
139
<ul>
 
140
<li><code>http://example.com/</code></li>
 
141
<li><code>http://example.com/fred</code></li>
 
142
<li><code>http://example.com/bob</code></li>
 
143
<li><code>http://example.com/fred/</code></li>
 
144
<li><code>http://example.com/bob/</code></li>
 
145
 
 
146
</ul>
 
147
 
 
148
<h3>.rpy scripts<a name="auto5"/></h3>
 
149
<a name="rpys" shape="rect"/>
 
150
 
 
151
<p>Files with the extension <code>.rpy</code> are python scripts which, when placed in a directory served by Twisted Web, will be executed when visited through the web.</p>
 
152
 
 
153
<p>An <code>.rpy</code> script must define a variable, <code>resource</code>, which is the Resource object that will render the request.</p>
 
154
 
 
155
<p><code>.rpy</code> files are very convenient for rapid development and prototyping. Since they are executed on every web request, defining a Resource subclass in an <code>.rpy</code> will make viewing the results of changes to your class visible simply by refreshing the page:</p>
 
156
 
 
157
<pre class="python"><p class="py-linenumber">1
 
158
2
 
159
3
 
160
4
 
161
5
 
162
6
 
163
7
 
164
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">resource</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Resource</span>
 
165
 
 
166
<span class="py-src-keyword">class</span> <span class="py-src-identifier">MyResource</span>(<span class="py-src-parameter">Resource</span>):
 
167
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
 
168
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;&lt;html&gt;Hello, world!&lt;/html&gt;&quot;</span>
 
169
 
 
170
<span class="py-src-variable">resource</span> = <span class="py-src-variable">MyResource</span>()
 
171
</pre>
 
172
 
 
173
<p>However, it is often a better idea to define Resource subclasses in Python modules. In order for changes in modules to be visible, you must either restart the Python process, or reload the module:</p>
 
174
 
 
175
<pre class="python"><p class="py-linenumber">1
 
176
2
 
177
3
 
178
4
 
179
5
 
180
6
 
181
</p><span class="py-src-keyword">import</span> <span class="py-src-variable">myresource</span>
 
182
 
 
183
<span class="py-src-comment">## Comment out this line when finished debugging</span>
 
184
<span class="py-src-variable">reload</span>(<span class="py-src-variable">myresource</span>)
 
185
 
 
186
<span class="py-src-variable">resource</span> = <span class="py-src-variable">myresource</span>.<span class="py-src-variable">MyResource</span>()
 
187
</pre>
 
188
 
 
189
<p>Creating a Twisted Web server which serves a directory is easy:</p>
 
190
 
 
191
<pre class="shell" xml:space="preserve">
 
192
% twistd -n web --path /Users/dsp/Sites
 
193
</pre>
 
194
 
 
195
<h3>Resource rendering<a name="auto6"/></h3>
 
196
<a name="rendering" shape="rect"/>
 
197
 
 
198
<p>Resource rendering occurs when Twisted Web locates a leaf Resource object to handle a web request. A Resource's <code>render</code> method may do various things to produce output which will be sent back to the browser:</p>
 
199
 
 
200
<ul>
 
201
<li>Return a string</li>
 
202
<li>Call <code>request.write(&quot;stuff&quot;)</code> as many times as desired, then call <code>request.finish()</code> and return <code>server.NOT_DONE_YET</code> (This is deceptive, since you are in fact done with the request, but is the correct way to do this)</li>
 
203
 
 
204
<li>Request a <code>Deferred</code>, return <code>server.NOT_DONE_YET</code>, and call <code>request.write(&quot;stuff&quot;)</code> and <code>request.finish()</code> later, in a callback on the <code>Deferred</code>.</li>
 
205
</ul>
 
206
 
 
207
<p>
 
208
 
 
209
The <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.resource.Resource.html" title="twisted.web.resource.Resource">Resource</a></code>
 
210
class, which is usually what one's Resource classes subclass, has a
 
211
convenient default implementation of <code class="python">render</code>. It will call a method named <code class="python">self.render_METHOD</code> where <q>METHOD</q> is
 
212
whatever HTTP method was used to request this resource. Examples:
 
213
request_GET, request_POST, request_HEAD, and so on. It is recommended
 
214
that you have your resource classes subclass <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.resource.Resource.html" title="twisted.web.resource.Resource">Resource</a></code> and implement <code class="python">render_METHOD</code> methods as opposed to <code class="python">render</code> itself. Note that for certain resources,
 
215
<code class="python">request_POST = request_GET</code> may be
 
216
desirable in case one wants to process arguments passed to the
 
217
resource regardless of whether they used GET
 
218
(<code>?foo=bar&amp;baz=quux</code>, and so forth) or POST.
 
219
 
 
220
</p>
 
221
 
 
222
<h3>Session<a name="auto7"/></h3>
 
223
<a name="sessions" shape="rect"/>
 
224
 
 
225
<p>HTTP is a stateless protocol; every request-response is treated as an individual unit, distinguishable from any other request only by the URL requested. With the advent of Cookies in the mid nineties, dynamic web servers gained the ability to distinguish between requests coming from different <em>browser sessions</em> by sending a Cookie to a browser. The browser then sends this cookie whenever it makes a request to a web server, allowing the server to track which requests come from which browser session.</p>
 
226
 
 
227
<p>Twisted Web provides an abstraction of this browser-tracking behavior called the <em>Session object</em>. Calling <code>request.getSession()</code> checks to see if a session cookie has been set; if not, it creates a unique session id, creates a Session object, stores it in the Site, and returns it. If a session object already exists, the same session object is returned. In this way, you can store data specific to the session in the session object.</p>
 
228
 
 
229
<img src="../img/web-session.png"/>
 
230
 
 
231
<h2>Advanced Configuration<a name="auto8"/></h2>
 
232
 
 
233
<p>Non-trivial configurations of Twisted Web are achieved with Python
 
234
configuration files. This is a Python snippet which builds up a variable
 
235
called application. Usually, a <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.application.internet.TCPServer.html" title="twisted.application.internet.TCPServer">twisted.application.internet.TCPServer</a></code> instance will
 
236
be used to make the application listen on a TCP port (80, in case direct
 
237
web serving is desired), with the listener being a
 
238
<code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.server.Site.html" title="twisted.web.server.Site">twisted.web.server.Site</a></code>. The resulting file can then
 
239
be run with <code class="shell">twistd -y</code>. Alternatively a reactor
 
240
object can be used directly to make a runnable script.</p>
 
241
 
 
242
<p>The <code>Site</code> will wrap a <code>Resource</code> object -- the
 
243
root.</p>
 
244
 
 
245
<pre class="python"><p class="py-linenumber">1
 
246
2
 
247
3
 
248
4
 
249
5
 
250
6
 
251
7
 
252
8
 
253
9
 
254
</p><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>
 
255
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">static</span>, <span class="py-src-variable">server</span>
 
256
 
 
257
<span class="py-src-variable">root</span> = <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/var/www/htdocs&quot;</span>)
 
258
<span class="py-src-variable">application</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">Application</span>(<span class="py-src-string">'web'</span>)
 
259
<span class="py-src-variable">site</span> = <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
 
260
<span class="py-src-variable">sc</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">IServiceCollection</span>(<span class="py-src-variable">application</span>)
 
261
<span class="py-src-variable">i</span> = <span class="py-src-variable">internet</span>.<span class="py-src-variable">TCPServer</span>(<span class="py-src-number">80</span>, <span class="py-src-variable">site</span>)
 
262
<span class="py-src-variable">i</span>.<span class="py-src-variable">setServiceParent</span>(<span class="py-src-variable">sc</span>)
 
263
</pre>
 
264
 
 
265
<p>Most advanced configurations will be in the form of tweaking the
 
266
root resource object.</p>
 
267
 
 
268
<h3>Adding Children<a name="auto9"/></h3>
 
269
 
 
270
<p>Usually, the root's children will be based on the filesystem's contents.
 
271
It is possible to override the filesystem by explicit <code>putChild</code>
 
272
methods.</p>
 
273
 
 
274
<p>Here are two examples. The first one adds a <code>/doc</code> child
 
275
to serve the documentation of the installed packages, while the second
 
276
one adds a <code>cgi-bin</code> directory for CGI scripts.</p>
 
277
 
 
278
<pre class="python"><p class="py-linenumber">1
 
279
2
 
280
3
 
281
4
 
282
5
 
283
6
 
284
7
 
285
</p><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">reactor</span>
 
286
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">static</span>, <span class="py-src-variable">server</span>
 
287
 
 
288
<span class="py-src-variable">root</span> = <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/var/www/htdocs&quot;</span>)
 
289
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;doc&quot;</span>, <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/usr/share/doc&quot;</span>))
 
290
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">80</span>, <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>))
 
291
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
 
292
</pre>
 
293
 
 
294
<pre class="python"><p class="py-linenumber">1
 
295
2
 
296
3
 
297
4
 
298
5
 
299
6
 
300
7
 
301
</p><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">reactor</span>
 
302
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">static</span>, <span class="py-src-variable">server</span>, <span class="py-src-variable">twcgi</span>
 
303
 
 
304
<span class="py-src-variable">root</span> = <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/var/www/htdocs&quot;</span>)
 
305
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;cgi-bin&quot;</span>, <span class="py-src-variable">twcgi</span>.<span class="py-src-variable">CGIDirectory</span>(<span class="py-src-string">&quot;/var/www/cgi-bin&quot;</span>))
 
306
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">80</span>, <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>))
 
307
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
 
308
</pre>
 
309
 
 
310
<h3>Modifying File Resources<a name="auto10"/></h3>
 
311
 
 
312
<p><code>File</code> resources, be they root object or children thereof,
 
313
have two important attributes that often need to be modified:
 
314
<code>indexNames</code> and <code>processors</code>. <code>indexNames</code>
 
315
determines which files are treated as <q>index files</q> -- served
 
316
up when a directory is rendered. <code>processors</code> determine how
 
317
certain file extensions are treated.</p>
 
318
 
 
319
<p>Here is an example for both, creating a site where all <code>.rpy</code>
 
320
extensions are Resource Scripts, and which renders directories by
 
321
searching for a <code>index.rpy</code> file.</p>
 
322
 
 
323
<pre class="python"><p class="py-linenumber"> 1
 
324
 2
 
325
 3
 
326
 4
 
327
 5
 
328
 6
 
329
 7
 
330
 8
 
331
 9
 
332
10
 
333
11
 
334
</p><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>
 
335
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">static</span>, <span class="py-src-variable">server</span>, <span class="py-src-variable">script</span>
 
336
 
 
337
<span class="py-src-variable">root</span> = <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/var/www/htdocs&quot;</span>)
 
338
<span class="py-src-variable">root</span>.<span class="py-src-variable">indexNames</span>=[<span class="py-src-string">'index.rpy'</span>]
 
339
<span class="py-src-variable">root</span>.<span class="py-src-variable">processors</span> = {<span class="py-src-string">'.rpy'</span>: <span class="py-src-variable">script</span>.<span class="py-src-variable">ResourceScript</span>}
 
340
<span class="py-src-variable">application</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">Application</span>(<span class="py-src-string">'web'</span>)
 
341
<span class="py-src-variable">sc</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">IServiceCollection</span>(<span class="py-src-variable">application</span>)
 
342
<span class="py-src-variable">site</span> = <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
 
343
<span class="py-src-variable">i</span> = <span class="py-src-variable">internet</span>.<span class="py-src-variable">TCPServer</span>(<span class="py-src-number">80</span>, <span class="py-src-variable">site</span>)
 
344
<span class="py-src-variable">i</span>.<span class="py-src-variable">setServiceParent</span>(<span class="py-src-variable">sc</span>)
 
345
</pre>
 
346
 
 
347
<p><code>File</code> objects also have a method called <code>ignoreExt</code>.
 
348
This method can be used to give extension-less URLs to users, so that
 
349
implementation is hidden. Here is an example:</p>
 
350
 
 
351
<pre class="python"><p class="py-linenumber"> 1
 
352
 2
 
353
 3
 
354
 4
 
355
 5
 
356
 6
 
357
 7
 
358
 8
 
359
 9
 
360
10
 
361
11
 
362
</p><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>
 
363
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">static</span>, <span class="py-src-variable">server</span>, <span class="py-src-variable">script</span>
 
364
 
 
365
<span class="py-src-variable">root</span> = <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/var/www/htdocs&quot;</span>)
 
366
<span class="py-src-variable">root</span>.<span class="py-src-variable">ignoreExt</span>(<span class="py-src-string">&quot;.rpy&quot;</span>)
 
367
<span class="py-src-variable">root</span>.<span class="py-src-variable">processors</span> = {<span class="py-src-string">'.rpy'</span>: <span class="py-src-variable">script</span>.<span class="py-src-variable">ResourceScript</span>}
 
368
<span class="py-src-variable">application</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">Application</span>(<span class="py-src-string">'web'</span>)
 
369
<span class="py-src-variable">sc</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">IServiceCollection</span>(<span class="py-src-variable">application</span>)
 
370
<span class="py-src-variable">site</span> = <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
 
371
<span class="py-src-variable">i</span> = <span class="py-src-variable">internet</span>.<span class="py-src-variable">TCPServer</span>(<span class="py-src-number">80</span>, <span class="py-src-variable">site</span>)
 
372
<span class="py-src-variable">i</span>.<span class="py-src-variable">setServiceParent</span>(<span class="py-src-variable">sc</span>)
 
373
</pre>
 
374
 
 
375
<p>Now, a URL such as <code>/foo</code> might be served from a Resource
 
376
Script called <code>foo.rpy</code>, if no file by the name of <code>foo</code>
 
377
exists.</p>
 
378
 
 
379
<h3>Virtual Hosts<a name="auto11"/></h3>
 
380
 
 
381
<p>Virtual hosting is done via a special resource, that should be
 
382
used as the root resource -- <code>NameVirtualHost</code>.
 
383
<code>NameVirtualHost</code> has an attribute named <code>default</code>,
 
384
which holds the default website. If a different root for some other
 
385
name is desired, the <code>addHost</code> method should be called.</p>
 
386
 
 
387
<pre class="python"><p class="py-linenumber"> 1
 
388
 2
 
389
 3
 
390
 4
 
391
 5
 
392
 6
 
393
 7
 
394
 8
 
395
 9
 
396
10
 
397
11
 
398
12
 
399
13
 
400
14
 
401
15
 
402
16
 
403
17
 
404
18
 
405
19
 
406
20
 
407
21
 
408
22
 
409
23
 
410
24
 
411
25
 
412
</p><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>
 
413
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">static</span>, <span class="py-src-variable">server</span>, <span class="py-src-variable">vhost</span>, <span class="py-src-variable">script</span>
 
414
 
 
415
<span class="py-src-variable">root</span> = <span class="py-src-variable">vhost</span>.<span class="py-src-variable">NameVirtualHost</span>()
 
416
 
 
417
<span class="py-src-comment"># Add a default -- htdocs</span>
 
418
<span class="py-src-variable">root</span>.<span class="py-src-variable">default</span>=<span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/var/www/htdocs&quot;</span>)
 
419
 
 
420
<span class="py-src-comment"># Add a simple virtual host -- foo.com</span>
 
421
<span class="py-src-variable">root</span>.<span class="py-src-variable">addHost</span>(<span class="py-src-string">&quot;foo.com&quot;</span>, <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/var/www/foo&quot;</span>))
 
422
 
 
423
<span class="py-src-comment"># Add a simple virtual host -- bar.com</span>
 
424
<span class="py-src-variable">root</span>.<span class="py-src-variable">addHost</span>(<span class="py-src-string">&quot;bar.com&quot;</span>, <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/var/www/bar&quot;</span>))
 
425
 
 
426
<span class="py-src-comment"># The &quot;baz&quot; people want to use Resource Scripts in their web site</span>
 
427
<span class="py-src-variable">baz</span> = <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/var/www/baz&quot;</span>)
 
428
<span class="py-src-variable">baz</span>.<span class="py-src-variable">processors</span> = {<span class="py-src-string">'.rpy'</span>: <span class="py-src-variable">script</span>.<span class="py-src-variable">ResourceScript</span>}
 
429
<span class="py-src-variable">baz</span>.<span class="py-src-variable">ignoreExt</span>(<span class="py-src-string">'.rpy'</span>)
 
430
<span class="py-src-variable">root</span>.<span class="py-src-variable">addHost</span>(<span class="py-src-string">'baz'</span>, <span class="py-src-variable">baz</span>)
 
431
 
 
432
<span class="py-src-variable">application</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">Application</span>(<span class="py-src-string">'web'</span>)
 
433
<span class="py-src-variable">sc</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">IServiceCollection</span>(<span class="py-src-variable">application</span>)
 
434
<span class="py-src-variable">site</span> = <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
 
435
<span class="py-src-variable">i</span> = <span class="py-src-variable">internet</span>.<span class="py-src-variable">TCPServer</span>(<span class="py-src-number">80</span>, <span class="py-src-variable">site</span>)
 
436
<span class="py-src-variable">i</span>.<span class="py-src-variable">setServiceParent</span>(<span class="py-src-variable">sc</span>)
 
437
</pre>
 
438
 
 
439
<h3>Advanced Techniques<a name="auto12"/></h3>
 
440
 
 
441
<p>Since the configuration is a Python snippet, it is possible to
 
442
use the full power of Python. Here are some simple examples:</p>
 
443
 
 
444
<pre class="python"><p class="py-linenumber"> 1
 
445
 2
 
446
 3
 
447
 4
 
448
 5
 
449
 6
 
450
 7
 
451
 8
 
452
 9
 
453
10
 
454
11
 
455
12
 
456
13
 
457
14
 
458
15
 
459
</p><span class="py-src-comment"># No need for configuration of virtual hosts -- just make sure</span>
 
460
<span class="py-src-comment"># a directory /var/vhosts/&lt;vhost name&gt; exists:</span>
 
461
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">vhost</span>, <span class="py-src-variable">static</span>, <span class="py-src-variable">server</span>
 
462
<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>
 
463
 
 
464
<span class="py-src-variable">root</span> = <span class="py-src-variable">vhost</span>.<span class="py-src-variable">NameVirtualHost</span>()
 
465
<span class="py-src-variable">root</span>.<span class="py-src-variable">default</span> = <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/var/www/htdocs&quot;</span>)
 
466
<span class="py-src-keyword">for</span> <span class="py-src-variable">dir</span> <span class="py-src-keyword">in</span> <span class="py-src-variable">os</span>.<span class="py-src-variable">listdir</span>(<span class="py-src-string">&quot;/var/vhosts&quot;</span>):
 
467
    <span class="py-src-variable">root</span>.<span class="py-src-variable">addHost</span>(<span class="py-src-variable">dir</span>, <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-variable">os</span>.<span class="py-src-variable">path</span>.<span class="py-src-variable">join</span>(<span class="py-src-string">&quot;/var/vhosts&quot;</span>, <span class="py-src-variable">dir</span>)))
 
468
 
 
469
<span class="py-src-variable">application</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">Application</span>(<span class="py-src-string">'web'</span>)
 
470
<span class="py-src-variable">sc</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">IServiceCollection</span>(<span class="py-src-variable">application</span>)
 
471
<span class="py-src-variable">site</span> = <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
 
472
<span class="py-src-variable">i</span> = <span class="py-src-variable">internet</span>.<span class="py-src-variable">TCPServer</span>(<span class="py-src-number">80</span>, <span class="py-src-variable">site</span>)
 
473
<span class="py-src-variable">i</span>.<span class="py-src-variable">setServiceParent</span>(<span class="py-src-variable">sc</span>)
 
474
</pre>
 
475
 
 
476
<pre class="python"><p class="py-linenumber"> 1
 
477
 2
 
478
 3
 
479
 4
 
480
 5
 
481
 6
 
482
 7
 
483
 8
 
484
 9
 
485
10
 
486
11
 
487
12
 
488
</p><span class="py-src-comment"># Determine ports we listen on based on a file with numbers:</span>
 
489
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">vhost</span>, <span class="py-src-variable">static</span>, <span class="py-src-variable">server</span>
 
490
<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>
 
491
 
 
492
<span class="py-src-variable">root</span> = <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/var/www/htdocs&quot;</span>)
 
493
 
 
494
<span class="py-src-variable">site</span> = <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
 
495
<span class="py-src-variable">application</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">Application</span>(<span class="py-src-string">'web'</span>)
 
496
<span class="py-src-variable">serviceCollection</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">IServiceCollection</span>(<span class="py-src-variable">application</span>)
 
497
 
 
498
<span class="py-src-keyword">for</span> <span class="py-src-variable">num</span> <span class="py-src-keyword">in</span> <span class="py-src-variable">map</span>(<span class="py-src-variable">int</span>, <span class="py-src-variable">open</span>(<span class="py-src-string">&quot;/etc/web/ports&quot;</span>).<span class="py-src-variable">read</span>().<span class="py-src-variable">split</span>()):
 
499
    <span class="py-src-variable">serviceCollection</span>.<span class="py-src-variable">addCollection</span>(<span class="py-src-variable">internet</span>.<span class="py-src-variable">TCPServer</span>(<span class="py-src-variable">num</span>, <span class="py-src-variable">site</span>))
 
500
</pre>
 
501
 
 
502
 
 
503
<h2>Running a Twisted Web Server<a name="auto13"/></h2>
 
504
 
 
505
<p>In many cases, you'll end up repeating common usage patterns of
 
506
twisted.web. In those cases you'll probably want to use Twisted's
 
507
pre-configured web server setup.</p>
 
508
 
 
509
<p>The easiest way to run a Twisted Web server is with the Twisted Daemonizer.
 
510
For example, this command will run a web server which serves static files from
 
511
a particular directory:</p>
 
512
 
 
513
<pre class="shell" xml:space="preserve">
 
514
% twistd web --path /path/to/web/content
 
515
</pre>
 
516
 
 
517
<p>If you just want to serve content from your own home directory, the
 
518
following will do:</p>
 
519
 
 
520
<pre class="shell" xml:space="preserve">
 
521
% twistd web --path ~/public_html/
 
522
</pre>
 
523
 
 
524
<p>You can stop the server at any time by going back to the directory you
 
525
started it in and running the command:</p>
 
526
 
 
527
<pre class="shell" xml:space="preserve">
 
528
% kill `cat twistd.pid`
 
529
</pre>
 
530
 
 
531
<p> Some other configuration options are available as well:  </p>
 
532
 
 
533
<ul>
 
534
  <li> <code>--port</code>: Specify the port for the web
 
535
       server to listen on.  This defaults to 8080.  </li>
 
536
  <li> <code>--logfile</code>: Specify the path to the
 
537
       log file. </li>
 
538
</ul>
 
539
 
 
540
<p> The full set of options that are available can be seen with:  </p>
 
541
 
 
542
<pre class="shell" xml:space="preserve">
 
543
% twistd web --help
 
544
</pre>
 
545
 
 
546
<h3>Serving Flat HTML<a name="auto14"/></h3>
 
547
 
 
548
<p> Twisted.Web serves flat HTML files just as it does any other flat file.  </p>
 
549
 
 
550
<a name="ResourceScripts" shape="rect"/>
 
551
<h3>Resource Scripts<a name="auto15"/></h3>
 
552
 
 
553
<p> A Resource script is a Python file ending with the extension <code>.rpy</code>, which is required to create an instance of a (subclass of a) <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.resource.Resource.html" title="twisted.web.resource.Resource">twisted.web.resource.Resource</a></code>. </p>
 
554
 
 
555
<p> Resource scripts have 3 special variables: </p>
 
556
 
 
557
<ul>
 
558
  <li> <code class="py-src-identifier">__file__</code>: The name of the .rpy file, including the full path.  This variable is automatically defined and present within the namespace.  </li>
 
559
  <li> <code class="py-src-identifier">registry</code>: An object of class <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.static.Registry.html" title="twisted.web.static.Registry">static.Registry</a></code>. It can be used to access and set persistent data keyed by a class.</li>
 
560
  <li> <code class="py-src-identifier">resource</code>: The variable which must be defined by the script and set to the resource instance that will be used to render the page. </li>
 
561
</ul>
 
562
 
 
563
<p> A very simple Resource Script might look like:  </p>
 
564
 
 
565
<pre class="python"><p class="py-linenumber">1
 
566
2
 
567
3
 
568
4
 
569
5
 
570
6
 
571
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">resource</span>
 
572
<span class="py-src-keyword">class</span> <span class="py-src-identifier">MyGreatResource</span>(<span class="py-src-parameter">resource</span>.<span class="py-src-parameter">Resource</span>):
 
573
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
 
574
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;&lt;html&gt;foo&lt;/html&gt;&quot;</span>
 
575
 
 
576
<span class="py-src-variable">resource</span> = <span class="py-src-variable">MyGreatResource</span>()
 
577
</pre>
 
578
 
 
579
<p> A slightly more complicated resource script, which accesses some
 
580
persistent data, might look like:</p>
 
581
 
 
582
<pre class="python"><p class="py-linenumber"> 1
 
583
 2
 
584
 3
 
585
 4
 
586
 5
 
587
 6
 
588
 7
 
589
 8
 
590
 9
 
591
10
 
592
11
 
593
12
 
594
13
 
595
14
 
596
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">resource</span>
 
597
<span class="py-src-keyword">from</span> <span class="py-src-variable">SillyWeb</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Counter</span>
 
598
 
 
599
<span class="py-src-variable">counter</span> = <span class="py-src-variable">registry</span>.<span class="py-src-variable">getComponent</span>(<span class="py-src-variable">Counter</span>)
 
600
<span class="py-src-keyword">if</span> <span class="py-src-keyword">not</span> <span class="py-src-variable">counter</span>:
 
601
   <span class="py-src-variable">registry</span>.<span class="py-src-variable">setComponent</span>(<span class="py-src-variable">Counter</span>, <span class="py-src-variable">Counter</span>())
 
602
<span class="py-src-variable">counter</span> = <span class="py-src-variable">registry</span>.<span class="py-src-variable">getComponent</span>(<span class="py-src-variable">Counter</span>)
 
603
 
 
604
<span class="py-src-keyword">class</span> <span class="py-src-identifier">MyResource</span>(<span class="py-src-parameter">resource</span>.<span class="py-src-parameter">Resource</span>):
 
605
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
 
606
        <span class="py-src-variable">counter</span>.<span class="py-src-variable">increment</span>()
 
607
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;you are visitor %d&quot;</span> % <span class="py-src-variable">counter</span>.<span class="py-src-variable">getValue</span>()
 
608
 
 
609
<span class="py-src-variable">resource</span> = <span class="py-src-variable">MyResource</span>()
 
610
</pre>
 
611
 
 
612
<p> This is assuming you have the <code>SillyWeb.Counter</code> module,
 
613
implemented something like the following:</p>
 
614
 
 
615
<pre class="python"><p class="py-linenumber"> 1
 
616
 2
 
617
 3
 
618
 4
 
619
 5
 
620
 6
 
621
 7
 
622
 8
 
623
 9
 
624
10
 
625
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">Counter</span>:
 
626
 
 
627
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>):
 
628
        <span class="py-src-variable">self</span>.<span class="py-src-variable">value</span> = <span class="py-src-number">0</span>
 
629
 
 
630
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">increment</span>(<span class="py-src-parameter">self</span>):
 
631
        <span class="py-src-variable">self</span>.<span class="py-src-variable">value</span> += <span class="py-src-number">1</span>
 
632
 
 
633
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">getValue</span>(<span class="py-src-parameter">self</span>):
 
634
        <span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">value</span>
 
635
</pre>
 
636
 
 
637
<h3>Web UIs<a name="auto16"/></h3>
 
638
 
 
639
<p>
 
640
The
 
641
<a href="http://www.divmod.org/projects/nevow" shape="rect">Nevow</a> framework, available as
 
642
part of the <a href="http://www.divmod.org/projects/quotient" shape="rect">Quotient</a> project,
 
643
is an advanced system for giving Web UIs to your application. Nevow uses Twisted Web but is
 
644
not itself part of Twisted.
 
645
</p>
 
646
 
 
647
<a name="SpreadableWebServers" shape="rect"/>
 
648
<h3>Spreadable Web Servers<a name="auto17"/></h3>
 
649
 
 
650
<p> One of the most interesting applications of Twisted.Web is the distributed webserver; multiple servers can all answer requests on the same port, using the <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.spread.html" title="twisted.spread">twisted.spread</a></code> package for <q>spreadable</q> computing.  In two different directories, run the commands:  </p>
 
651
 
 
652
<pre class="shell" xml:space="preserve">
 
653
% twistd web --user
 
654
% twistd web --personal [other options, if you desire]
 
655
</pre>
 
656
 
 
657
<p> Once you're running both of these instances, go to <code>http://localhost:8080/your_username.twistd/</code> -- you will see the front page from the server you created with the <code>--personal</code> option.  What's happening here is that the request you've sent is being relayed from the central (User) server to your own (Personal) server, over a PB connection.  This technique can be highly useful for small <q>community</q> sites; using the code that makes this demo work, you can connect one HTTP port to multiple resources running with different permissions on the same machine, on different local machines, or even over the internet to a remote site.  </p>
 
658
 
 
659
<p>
 
660
By default, a personal server listens on a UNIX socket in the owner's home
 
661
directory.  The <code class="shell">--port</code> option can be used to make
 
662
it listen on a different address, such as a TCP or SSL server or on a UNIX
 
663
server in a different location.  If you use this option to make a personal
 
664
server listen on a different address, the central (User) server won't be
 
665
able to find it, but a custom server which uses the same APIs as the central
 
666
server might.  Another use of the <code class="shell">--port</code> option
 
667
is to make the UNIX server robust against system crashes.  If the server
 
668
crashes and the UNIX socket is left on the filesystem, the personal server
 
669
will not be able to restart until it is removed.  However, if <code class="shell">--port unix:/home/username/.twistd-web-pb:wantPID=1</code> is
 
670
supplied when creating the personal server, then a lockfile will be used to
 
671
keep track of whether the server socket is in use and automatically delete
 
672
it when it is not.
 
673
</p>
 
674
 
 
675
<h3>Serving PHP/Perl/CGI<a name="auto18"/></h3>
 
676
 
 
677
<p>Everything related to CGI is located in the
 
678
<code>twisted.web.twcgi</code>, and it's here you'll find the classes that you
 
679
need to subclass in order to support the language of your (or somebody elses)
 
680
taste. You'll also need to create your own kind of resource if you are using a
 
681
non-unix operatingsystem (such as Windows), or if the default resources has
 
682
wrong pathnames to the parsers.</p>
 
683
 
 
684
<p>The following snippet is a .rpy that serves perl-files. Look at <code>twisted.web.twcgi</code>
 
685
for more examples regarding twisted.web and CGI.</p>
 
686
 
 
687
<pre class="python"><p class="py-linenumber">1
 
688
2
 
689
3
 
690
4
 
691
5
 
692
6
 
693
7
 
694
8
 
695
9
 
696
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">static</span>, <span class="py-src-variable">twcgi</span>
 
697
 
 
698
<span class="py-src-keyword">class</span> <span class="py-src-identifier">PerlScript</span>(<span class="py-src-parameter">twcgi</span>.<span class="py-src-parameter">FilteredScript</span>):
 
699
    <span class="py-src-variable">filter</span> = <span class="py-src-string">'/usr/bin/perl'</span> <span class="py-src-comment"># Points to the perl parser</span>
 
700
 
 
701
<span class="py-src-variable">resource</span> = <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/perlsite&quot;</span>) <span class="py-src-comment"># Points to the perl website</span>
 
702
<span class="py-src-variable">resource</span>.<span class="py-src-variable">processors</span> = {<span class="py-src-string">&quot;.pl&quot;</span>: <span class="py-src-variable">PerlScript</span>} <span class="py-src-comment"># Files that end with .pl will be</span>
 
703
                                          <span class="py-src-comment"># processed by PerlScript</span>
 
704
<span class="py-src-variable">resource</span>.<span class="py-src-variable">indexNames</span> = [<span class="py-src-string">'index.pl'</span>]
 
705
</pre>
 
706
 
 
707
<h3>Serving WSGI Applications<a name="auto19"/></h3>
 
708
 
 
709
<p><a href="http://wsgi.org/wsgi/" shape="rect">WSGI</a> is the Web Server Gateway
 
710
Interface. It is a specification for web servers and application servers to
 
711
communicate with Python web applications. All modern Python web frameworks
 
712
support the WSGI interface.</p>
 
713
 
 
714
<p>The easiest way to get started with WSGI application is to use the twistd
 
715
command:</p>
 
716
 
 
717
<pre class="shell" xml:space="preserve">
 
718
% twistd -n web --wsgi=helloworld.application
 
719
</pre>
 
720
 
 
721
<p>This assumes that you have a WSGI application called application in
 
722
your helloworld module/package, which might look like this:</p>
 
723
 
 
724
<pre class="python"><p class="py-linenumber">1
 
725
2
 
726
3
 
727
4
 
728
</p><span class="py-src-keyword">def</span> <span class="py-src-identifier">application</span>(<span class="py-src-parameter">environ</span>, <span class="py-src-parameter">start_response</span>):
 
729
    <span class="py-src-string">&quot;&quot;&quot;Basic WSGI Application&quot;&quot;&quot;</span>
 
730
    <span class="py-src-variable">start_response</span>(<span class="py-src-string">'200 OK'</span>, [(<span class="py-src-string">'Content-type'</span>,<span class="py-src-string">'text/plain'</span>)])
 
731
    <span class="py-src-keyword">return</span> [<span class="py-src-string">'Hello World!'</span>]
 
732
</pre>
 
733
 
 
734
<p>The above setup will be suitable for many applications where all that is
 
735
needed is to server the WSGI application at the site's root. However, for
 
736
greater control, Twisted provides support for using WSGI applications as
 
737
resources <code class="api">twisted.web.wsgi.WSGIResource</code>.</p>
 
738
 
 
739
<p>Here is an example of a WSGI application being served as the root resource
 
740
for a site, in the following tac file:</p>
 
741
 
 
742
<pre class="python"><p class="py-linenumber"> 1
 
743
 2
 
744
 3
 
745
 4
 
746
 5
 
747
 6
 
748
 7
 
749
 8
 
750
 9
 
751
10
 
752
11
 
753
12
 
754
13
 
755
14
 
756
15
 
757
16
 
758
17
 
759
18
 
760
19
 
761
20
 
762
21
 
763
22
 
764
23
 
765
24
 
766
25
 
767
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">server</span>
 
768
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">wsgi</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">WSGIResource</span>
 
769
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">threadpool</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">ThreadPool</span>
 
770
<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">reactor</span>
 
771
<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">service</span>, <span class="py-src-variable">strports</span>
 
772
 
 
773
<span class="py-src-comment"># Create and start a thread pool,</span>
 
774
<span class="py-src-variable">wsgiThreadPool</span> = <span class="py-src-variable">ThreadPool</span>()
 
775
<span class="py-src-variable">wsgiThreadPool</span>.<span class="py-src-variable">start</span>()
 
776
 
 
777
<span class="py-src-comment"># ensuring that it will be stopped when the reactor shuts down</span>
 
778
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">addSystemEventTrigger</span>(<span class="py-src-string">'after'</span>, <span class="py-src-string">'shutdown'</span>, <span class="py-src-variable">wsgiThreadPool</span>.<span class="py-src-variable">stop</span>)
 
779
 
 
780
<span class="py-src-keyword">def</span> <span class="py-src-identifier">application</span>(<span class="py-src-parameter">environ</span>, <span class="py-src-parameter">start_response</span>):
 
781
    <span class="py-src-string">&quot;&quot;&quot;A basic WSGI application&quot;&quot;&quot;</span>
 
782
    <span class="py-src-variable">start_response</span>(<span class="py-src-string">'200 OK'</span>, [(<span class="py-src-string">'Content-type'</span>,<span class="py-src-string">'text/plain'</span>)])
 
783
    <span class="py-src-keyword">return</span> [<span class="py-src-string">'Hello World!'</span>]
 
784
 
 
785
<span class="py-src-comment"># Create the WSGI resource</span>
 
786
<span class="py-src-variable">wsgiAppAsResource</span> = <span class="py-src-variable">WSGIResource</span>(<span class="py-src-variable">reactor</span>, <span class="py-src-variable">wsgiThreadPool</span>, <span class="py-src-variable">application</span>)
 
787
 
 
788
<span class="py-src-comment"># Hooks for twistd</span>
 
789
<span class="py-src-variable">application</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">Application</span>(<span class="py-src-string">'Twisted.web.wsgi Hello World Example'</span>)
 
790
<span class="py-src-variable">server</span> = <span class="py-src-variable">strports</span>.<span class="py-src-variable">service</span>(<span class="py-src-string">'tcp:8080'</span>, <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">wsgiAppAsResource</span>))
 
791
<span class="py-src-variable">server</span>.<span class="py-src-variable">setServiceParent</span>(<span class="py-src-variable">application</span>)
 
792
</pre>
 
793
 
 
794
<p>This can then be run like any other .tac file:</p>
 
795
 
 
796
<pre class="shell" xml:space="preserve">
 
797
% twistd -ny myapp.tac
 
798
</pre>
 
799
 
 
800
<p>Because of the synchronous nature of WSGI, each application call (for
 
801
each request) is called within a thread, and the result is written back to the
 
802
web server. For this, a <code class="api">twisted.python.threadpool.ThreadPool</code>
 
803
instance is used.</p>
 
804
 
 
805
<h3>Using VHostMonster<a name="auto20"/></h3>
 
806
 
 
807
<p>It is common to use one server (for example, Apache) on a site with multiple
 
808
names which then uses reverse proxy (in Apache, via <code>mod_proxy</code>) to different
 
809
internal web servers, possibly on different machines. However, naive
 
810
configuration causes miscommunication: the internal server firmly believes it
 
811
is running on <q>internal-name:port</q>, and will generate URLs to that effect,
 
812
which will be completely wrong when received by the client.</p>
 
813
 
 
814
<p>While Apache has the ProxyPassReverse directive, it is really a hack
 
815
and is nowhere near comprehensive enough. Instead, the recommended practice
 
816
in case the internal web server is Twisted.Web is to use VHostMonster.</p>
 
817
 
 
818
<p>From the Twisted side, using VHostMonster is easy: just drop a file named
 
819
(for example) <code>vhost.rpy</code> containing the following:</p>
 
820
 
 
821
<pre class="python"><p class="py-linenumber">1
 
822
2
 
823
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">vhost</span>
 
824
<span class="py-src-variable">resource</span> = <span class="py-src-variable">vhost</span>.<span class="py-src-variable">VHostMonsterResource</span>()
 
825
</pre>
 
826
 
 
827
<p>Of course, an equivalent <code>.trp</code> can also be used. Make sure
 
828
the web server is configured with the correct processors for the
 
829
<code>rpy</code> or <code>trp</code> extensions (the web server
 
830
<code>twistd web --path</code> generates by default is so configured).</p>
 
831
 
 
832
<p>From the Apache side, instead of using the following ProxyPass directive:</p>
 
833
 
 
834
<pre xml:space="preserve">
 
835
&lt;VirtualHost ip-addr&gt;
 
836
ProxyPass / http://localhost:8538/
 
837
ServerName example.com
 
838
&lt;/VirtualHost&gt;
 
839
</pre>
 
840
 
 
841
<p>Use the following directive:</p>
 
842
 
 
843
<pre xml:space="preserve">
 
844
&lt;VirtualHost ip-addr&gt;
 
845
ProxyPass / http://localhost:8538/vhost.rpy/http/example.com:80/
 
846
ServerName example.com
 
847
&lt;/VirtualHost&gt;
 
848
</pre>
 
849
 
 
850
<p>Here is an example for Twisted.Web's reverse proxy:</p>
 
851
 
 
852
<pre class="python"><p class="py-linenumber"> 1
 
853
 2
 
854
 3
 
855
 4
 
856
 5
 
857
 6
 
858
 7
 
859
 8
 
860
 9
 
861
10
 
862
11
 
863
12
 
864
</p><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>
 
865
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">proxy</span>, <span class="py-src-variable">server</span>, <span class="py-src-variable">vhost</span>
 
866
<span class="py-src-variable">vhostName</span> = <span class="py-src-string">'example.com'</span>
 
867
<span class="py-src-variable">reverseProxy</span> = <span class="py-src-variable">proxy</span>.<span class="py-src-variable">ReverseProxyResource</span>(<span class="py-src-string">'internal'</span>, <span class="py-src-number">8538</span>,
 
868
                                          <span class="py-src-string">'/vhost.rpy/http/'</span>+<span class="py-src-variable">vhostName</span>+<span class="py-src-string">'/'</span>)
 
869
<span class="py-src-variable">root</span> = <span class="py-src-variable">vhost</span>.<span class="py-src-variable">NameVirtualHost</span>()
 
870
<span class="py-src-variable">root</span>.<span class="py-src-variable">addHost</span>(<span class="py-src-variable">vhostName</span>, <span class="py-src-variable">reverseProxy</span>)
 
871
<span class="py-src-variable">site</span> = <span class="py-src-variable">server</span>.<span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
 
872
<span class="py-src-variable">application</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">Application</span>(<span class="py-src-string">'web-proxy'</span>)
 
873
<span class="py-src-variable">sc</span> = <span class="py-src-variable">service</span>.<span class="py-src-variable">IServiceCollection</span>(<span class="py-src-variable">application</span>)
 
874
<span class="py-src-variable">i</span> = <span class="py-src-variable">internet</span>.<span class="py-src-variable">TCPServer</span>(<span class="py-src-number">80</span>, <span class="py-src-variable">site</span>)
 
875
<span class="py-src-variable">i</span>.<span class="py-src-variable">setServiceParent</span>(<span class="py-src-variable">sc</span>)
 
876
</pre>
 
877
 
 
878
<h2>Rewriting URLs<a name="auto21"/></h2>
 
879
 
 
880
<p>Sometimes it is convenient to modify the content of the
 
881
<code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.server.Request.html" title="twisted.web.server.Request">Request</a></code> object
 
882
before passing it on. Because this is most often used to rewrite
 
883
either the URL, the similarity to Apache's <code>mod_rewrite</code> has
 
884
inspired the <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.rewrite.html" title="twisted.web.rewrite">twisted.web.rewrite</a></code> module. Using
 
885
this module is done via wrapping a resource with a
 
886
<code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.rewrite.RewriterResource.html" title="twisted.web.rewrite.RewriterResource">twisted.web.rewrite.RewriterResource</a></code> which
 
887
then has rewrite rules. Rewrite rules are functions which accept a request
 
888
object, and possible modify it. After all rewrite rules run, the child
 
889
resolution chain continues as if the wrapped resource, rather than
 
890
the <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.rewrite.RewriterResource.html" title="twisted.web.rewrite.RewriterResource">RewriterResource</a></code>,
 
891
was the child.</p>
 
892
 
 
893
<p>Here is an example, using the only rule currently supplied by Twisted
 
894
itself:</p>
 
895
 
 
896
<pre class="python"><p class="py-linenumber">1
 
897
</p><span class="py-src-variable">default_root</span> = <span class="py-src-variable">rewrite</span>.<span class="py-src-variable">RewriterResource</span>(<span class="py-src-variable">default</span>, <span class="py-src-variable">rewrite</span>.<span class="py-src-variable">tildeToUsers</span>)
 
898
</pre>
 
899
 
 
900
<p>This causes the URL <code>/~foo/bar.html</code> to be treated like
 
901
<code>/users/foo/bar.html</code>. If done after setting default's
 
902
<code>users</code> child to a
 
903
<code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.distrib.UserDirectory.html" title="twisted.web.distrib.UserDirectory">distrib.UserDirectory</a></code>,
 
904
it gives a configuration similar to the classical configuration of
 
905
web server, common since the first NCSA servers.</p>
 
906
 
 
907
<h2>Knowing When We're Not Wanted<a name="auto22"/></h2>
 
908
 
 
909
<p>Sometimes it is useful to know when the other side has broken the connection.
 
910
Here is an example which does that:</p>
 
911
 
 
912
<pre class="python"><p class="py-linenumber"> 1
 
913
 2
 
914
 3
 
915
 4
 
916
 5
 
917
 6
 
918
 7
 
919
 8
 
920
 9
 
921
10
 
922
11
 
923
12
 
924
13
 
925
14
 
926
15
 
927
16
 
928
17
 
929
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">resource</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Resource</span>
 
930
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">server</span>
 
931
<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">reactor</span>
 
932
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">util</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">println</span>
 
933
 
 
934
 
 
935
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ExampleResource</span>(<span class="py-src-parameter">Resource</span>):
 
936
 
 
937
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
 
938
        <span class="py-src-variable">request</span>.<span class="py-src-variable">write</span>(<span class="py-src-string">&quot;hello world&quot;</span>)
 
939
        <span class="py-src-variable">d</span> = <span class="py-src-variable">request</span>.<span class="py-src-variable">notifyFinish</span>()
 
940
        <span class="py-src-variable">d</span>.<span class="py-src-variable">addCallback</span>(<span class="py-src-keyword">lambda</span> <span class="py-src-variable">_</span>: <span class="py-src-variable">println</span>(<span class="py-src-string">&quot;finished normally&quot;</span>))
 
941
        <span class="py-src-variable">d</span>.<span class="py-src-variable">addErrback</span>(<span class="py-src-variable">println</span>, <span class="py-src-string">&quot;error&quot;</span>)
 
942
        <span class="py-src-variable">reactor</span>.<span class="py-src-variable">callLater</span>(<span class="py-src-number">10</span>, <span class="py-src-variable">request</span>.<span class="py-src-variable">finish</span>)
 
943
        <span class="py-src-keyword">return</span> <span class="py-src-variable">server</span>.<span class="py-src-variable">NOT_DONE_YET</span>
 
944
 
 
945
<span class="py-src-variable">resource</span> = <span class="py-src-variable">ExampleResource</span>()
 
946
</pre>
 
947
 
 
948
<p>This will allow us to run statistics on the log-file to see how many users
 
949
are frustrated after merely 10 seconds.</p>
 
950
 
 
951
<h2>As-Is Serving<a name="auto23"/></h2>
 
952
 
 
953
<p>Sometimes, you want to be able to send headers and status directly. While
 
954
you can do this with a
 
955
<code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.script.ResourceScript.html" title="twisted.web.script.ResourceScript">ResourceScript</a></code>, an easier
 
956
way is to use <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.static.AsIsProcessor.html" title="twisted.web.static.AsIsProcessor">AsIsProcessor</a></code>.
 
957
Use it by, for example, addding it as a processor for the <code>.asis</code>
 
958
extension. Here is a sample file:</p>
 
959
 
 
960
<pre xml:space="preserve">
 
961
HTTP/1.0 200 OK
 
962
Content-Type: text/html
 
963
 
 
964
Hello world
 
965
</pre>
 
966
 
 
967
</div>
 
968
 
 
969
    <p><a href="index.html">Index</a></p>
 
970
    <span class="version">Version: 10.0.0</span>
 
971
  </body>
 
972
</html>
 
 
b'\\ No newline at end of file'