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">
3
<title>Twisted Documentation: Configuring and Using the Twisted.Web Server</title>
4
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
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>
13
<h2>Twisted Web Development<a name="auto0"/></h2><a name="development" shape="rect"/>
15
<p>Twisted Web serves Python objects that implement the interface
18
<br clear="none"/><img alt="Twisted Web process" src="../img/web-process.png"/>
20
<h3>Main Concepts<a name="auto1"/></h3>
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>
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>
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>
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>
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>
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>
39
<p>The Twisted.Web server is started through the Twisted Daemonizer, as in:</p>
41
<pre class="shell" xml:space="preserve">
45
<h3>Site Objects<a name="auto2"/></h3>
46
<a name="sites" shape="rect"/>
48
<p>Site objects serve as the glue between a port to listen for HTTP requests on, and a root Resource object.</p>
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>
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>
55
<pre class="python"><p class="py-linenumber"> 1
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>
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">"<html>Hello, world!</html>"</span>
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>()
79
<h3>Resource objects<a name="auto3"/></h3>
80
<a name="resources" shape="rect"/>
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>
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>
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>
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>
90
<p>A Resource can know which path segments will be processed after it by looking at <code>request.postpath</code>.</p>
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>
94
<p>Here is a simple Resource object:</p>
96
<pre class="python"><p class="py-linenumber"> 1
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>
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>)
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">"Hello, world! I am located at %r."</span> % (<span class="py-src-variable">request</span>.<span class="py-src-variable">prepath</span>,)
121
<span class="py-src-variable">resource</span> = <span class="py-src-variable">Hello</span>()
124
<h3>Resource Trees<a name="auto4"/></h3>
125
<a name="trees" shape="rect"/>
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>
129
<pre class="python"><p class="py-linenumber">1
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>())
137
<p>If this root resource is served as the root of a Site instance, the following URLs will all be valid:</p>
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>
148
<h3>.rpy scripts<a name="auto5"/></h3>
149
<a name="rpys" shape="rect"/>
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>
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>
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>
157
<pre class="python"><p class="py-linenumber">1
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>
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">"<html>Hello, world!</html>"</span>
170
<span class="py-src-variable">resource</span> = <span class="py-src-variable">MyResource</span>()
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>
175
<pre class="python"><p class="py-linenumber">1
181
</p><span class="py-src-keyword">import</span> <span class="py-src-variable">myresource</span>
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>)
186
<span class="py-src-variable">resource</span> = <span class="py-src-variable">myresource</span>.<span class="py-src-variable">MyResource</span>()
189
<p>Creating a Twisted Web server which serves a directory is easy:</p>
191
<pre class="shell" xml:space="preserve">
192
% twistd -n web --path /Users/dsp/Sites
195
<h3>Resource rendering<a name="auto6"/></h3>
196
<a name="rendering" shape="rect"/>
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>
201
<li>Return a string</li>
202
<li>Call <code>request.write("stuff")</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>
204
<li>Request a <code>Deferred</code>, return <code>server.NOT_DONE_YET</code>, and call <code>request.write("stuff")</code> and <code>request.finish()</code> later, in a callback on the <code>Deferred</code>.</li>
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&baz=quux</code>, and so forth) or POST.
222
<h3>Session<a name="auto7"/></h3>
223
<a name="sessions" shape="rect"/>
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>
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>
229
<img src="../img/web-session.png"/>
231
<h2>Advanced Configuration<a name="auto8"/></h2>
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>
242
<p>The <code>Site</code> will wrap a <code>Resource</code> object -- the
245
<pre class="python"><p class="py-linenumber">1
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>
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">"/var/www/htdocs"</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>)
265
<p>Most advanced configurations will be in the form of tweaking the
266
root resource object.</p>
268
<h3>Adding Children<a name="auto9"/></h3>
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>
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>
278
<pre class="python"><p class="py-linenumber">1
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>
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">"/var/www/htdocs"</span>)
289
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">"doc"</span>, <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">"/usr/share/doc"</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>()
294
<pre class="python"><p class="py-linenumber">1
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>
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">"/var/www/htdocs"</span>)
305
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">"cgi-bin"</span>, <span class="py-src-variable">twcgi</span>.<span class="py-src-variable">CGIDirectory</span>(<span class="py-src-string">"/var/www/cgi-bin"</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>()
310
<h3>Modifying File Resources<a name="auto10"/></h3>
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>
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>
323
<pre class="python"><p class="py-linenumber"> 1
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>
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">"/var/www/htdocs"</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>)
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>
351
<pre class="python"><p class="py-linenumber"> 1
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>
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">"/var/www/htdocs"</span>)
366
<span class="py-src-variable">root</span>.<span class="py-src-variable">ignoreExt</span>(<span class="py-src-string">".rpy"</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>)
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>
379
<h3>Virtual Hosts<a name="auto11"/></h3>
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>
387
<pre class="python"><p class="py-linenumber"> 1
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>
415
<span class="py-src-variable">root</span> = <span class="py-src-variable">vhost</span>.<span class="py-src-variable">NameVirtualHost</span>()
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">"/var/www/htdocs"</span>)
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">"foo.com"</span>, <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">"/var/www/foo"</span>))
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">"bar.com"</span>, <span class="py-src-variable">static</span>.<span class="py-src-variable">File</span>(<span class="py-src-string">"/var/www/bar"</span>))
426
<span class="py-src-comment"># The "baz" 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">"/var/www/baz"</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>)
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>)
439
<h3>Advanced Techniques<a name="auto12"/></h3>
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>
444
<pre class="python"><p class="py-linenumber"> 1
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/<vhost name> 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>
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">"/var/www/htdocs"</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">"/var/vhosts"</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">"/var/vhosts"</span>, <span class="py-src-variable">dir</span>)))
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>)
476
<pre class="python"><p class="py-linenumber"> 1
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>
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">"/var/www/htdocs"</span>)
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>)
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">"/etc/web/ports"</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>))
503
<h2>Running a Twisted Web Server<a name="auto13"/></h2>
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>
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>
513
<pre class="shell" xml:space="preserve">
514
% twistd web --path /path/to/web/content
517
<p>If you just want to serve content from your own home directory, the
518
following will do:</p>
520
<pre class="shell" xml:space="preserve">
521
% twistd web --path ~/public_html/
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>
527
<pre class="shell" xml:space="preserve">
528
% kill `cat twistd.pid`
531
<p> Some other configuration options are available as well: </p>
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
540
<p> The full set of options that are available can be seen with: </p>
542
<pre class="shell" xml:space="preserve">
546
<h3>Serving Flat HTML<a name="auto14"/></h3>
548
<p> Twisted.Web serves flat HTML files just as it does any other flat file. </p>
550
<a name="ResourceScripts" shape="rect"/>
551
<h3>Resource Scripts<a name="auto15"/></h3>
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>
555
<p> Resource scripts have 3 special variables: </p>
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>
563
<p> A very simple Resource Script might look like: </p>
565
<pre class="python"><p class="py-linenumber">1
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">"<html>foo</html>"</span>
576
<span class="py-src-variable">resource</span> = <span class="py-src-variable">MyGreatResource</span>()
579
<p> A slightly more complicated resource script, which accesses some
580
persistent data, might look like:</p>
582
<pre class="python"><p class="py-linenumber"> 1
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>
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>)
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">"you are visitor %d"</span> % <span class="py-src-variable">counter</span>.<span class="py-src-variable">getValue</span>()
609
<span class="py-src-variable">resource</span> = <span class="py-src-variable">MyResource</span>()
612
<p> This is assuming you have the <code>SillyWeb.Counter</code> module,
613
implemented something like the following:</p>
615
<pre class="python"><p class="py-linenumber"> 1
625
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">Counter</span>:
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>
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>
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>
637
<h3>Web UIs<a name="auto16"/></h3>
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.
647
<a name="SpreadableWebServers" shape="rect"/>
648
<h3>Spreadable Web Servers<a name="auto17"/></h3>
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>
652
<pre class="shell" xml:space="preserve">
654
% twistd web --personal [other options, if you desire]
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>
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
675
<h3>Serving PHP/Perl/CGI<a name="auto18"/></h3>
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>
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>
687
<pre class="python"><p class="py-linenumber">1
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>
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>
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">"/perlsite"</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">".pl"</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>]
707
<h3>Serving WSGI Applications<a name="auto19"/></h3>
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>
714
<p>The easiest way to get started with WSGI application is to use the twistd
717
<pre class="shell" xml:space="preserve">
718
% twistd -n web --wsgi=helloworld.application
721
<p>This assumes that you have a WSGI application called application in
722
your helloworld module/package, which might look like this:</p>
724
<pre class="python"><p class="py-linenumber">1
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">"""Basic WSGI Application"""</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>]
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>
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>
742
<pre class="python"><p class="py-linenumber"> 1
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>
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>()
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>)
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">"""A basic WSGI application"""</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>]
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>)
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>)
794
<p>This can then be run like any other .tac file:</p>
796
<pre class="shell" xml:space="preserve">
797
% twistd -ny myapp.tac
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>
805
<h3>Using VHostMonster<a name="auto20"/></h3>
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>
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>
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>
821
<pre class="python"><p class="py-linenumber">1
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>()
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>
832
<p>From the Apache side, instead of using the following ProxyPass directive:</p>
834
<pre xml:space="preserve">
835
<VirtualHost ip-addr>
836
ProxyPass / http://localhost:8538/
837
ServerName example.com
841
<p>Use the following directive:</p>
843
<pre xml:space="preserve">
844
<VirtualHost ip-addr>
845
ProxyPass / http://localhost:8538/vhost.rpy/http/example.com:80/
846
ServerName example.com
850
<p>Here is an example for Twisted.Web's reverse proxy:</p>
852
<pre class="python"><p class="py-linenumber"> 1
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>)
878
<h2>Rewriting URLs<a name="auto21"/></h2>
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>,
893
<p>Here is an example, using the only rule currently supplied by Twisted
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>)
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>
907
<h2>Knowing When We're Not Wanted<a name="auto22"/></h2>
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>
912
<pre class="python"><p class="py-linenumber"> 1
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>
935
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ExampleResource</span>(<span class="py-src-parameter">Resource</span>):
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">"hello world"</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">"finished normally"</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">"error"</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>
945
<span class="py-src-variable">resource</span> = <span class="py-src-variable">ExampleResource</span>()
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>
951
<h2>As-Is Serving<a name="auto23"/></h2>
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>
960
<pre xml:space="preserve">
962
Content-Type: text/html
969
<p><a href="index.html">Index</a></p>
970
<span class="version">Version: 10.0.0</span>
b'\\ No newline at end of file'