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: Static URL Dispatch</title>
4
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
8
<h1 class="title">Static URL Dispatch</h1>
9
<div class="toc"><ol/></div>
13
<p>The goal of this example is to show you how to serve different content at
16
<p>The key to understanding how different URLs are handled with the resource
17
APIs in Twisted Web is understanding that any URL can be used to address a node
18
in a tree. Resources in Twisted Web exist in such a tree, and a request for a
19
URL will be responded to by the resource which that URL addresses. The
20
addressing scheme considers only the path segments of the URL. Starting with the
21
root resource (the one used to construct the <code>Site</code>) and the first
22
path segment, a child resource is looked up. As long as there are more path
23
segments, this process is repeated using the result of the previous lookup and
24
the next path segment. For example, to handle a request for
25
<code>/foo/bar</code>, first the root's "<code>foo</code>" child is retrieved,
26
then that resource's "<code>bar</code>" child is retrieved, then that resource
27
is used to create the response.</p>
29
<p>With that out of the way, let's consider an example that can serve a few
30
different resources at a few different URLs.</p>
32
<p>First things first: we need to import <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.server.Site.html" title="twisted.web.server.Site">Site</a></code>, the factory for HTTP servers, <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>, a convenient base class
33
for custom pages, and <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.internet.reactor.html" title="twisted.internet.reactor">reactor</a></code>,
34
the object which implements the Twisted main loop. We'll also import <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.static.File.html" title="twisted.web.static.File">File</a></code> to use as the resource at one
35
of the example URLs.</p>
37
<pre class="python"><p class="py-linenumber">1
41
</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">server</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Site</span>
42
<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>
43
<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>
44
<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">static</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">File</span>
47
<p>Now we create a resource which will correspond to the root of the URL
48
hierarchy: all URLs are children of this resource.</p>
50
<pre class="python"><p class="py-linenumber">1
51
</p><span class="py-src-variable">root</span> = <span class="py-src-variable">Resource</span>()
54
<p>Here comes the interesting part of this example. We're now going to create
55
three more resources and attach them to the three URLs <code>/foo</code>,
56
<code>/bar</code>, and <code>/baz</code>:</p>
58
<pre class="python"><p class="py-linenumber">1
61
</p><span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">"foo"</span>, <span class="py-src-variable">File</span>(<span class="py-src-string">"/tmp"</span>))
62
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">"bar"</span>, <span class="py-src-variable">File</span>(<span class="py-src-string">"/lost+found"</span>))
63
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">"baz"</span>, <span class="py-src-variable">File</span>(<span class="py-src-string">"/opt"</span>))
66
<p>Last, all that's required is to create a <code>Site</code> with the root
67
resource, associate it with a listening server port, and start the reactor:</p>
69
<pre class="python"><p class="py-linenumber">1
72
</p><span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
73
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">8880</span>, <span class="py-src-variable">factory</span>)
74
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
77
<p>With this server running, <code>http://localhost:8880/foo</code> will serve a
78
listing of files from <code>/tmp</code>, <code>http://localhost:8880/bar</code>
79
will serve a listing of files from <code>/lost+found</code>, and
80
<code>http://localhost:8880/baz</code> will serve a listing of files from
81
<code>/opt</code>.</p>
83
<p>Here's the whole example uninterrupted:</p>
85
<pre class="python"><p class="py-linenumber"> 1
98
</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">server</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Site</span>
99
<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>
100
<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>
101
<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">static</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">File</span>
103
<span class="py-src-variable">root</span> = <span class="py-src-variable">Resource</span>()
104
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">"foo"</span>, <span class="py-src-variable">File</span>(<span class="py-src-string">"/tmp"</span>))
105
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">"bar"</span>, <span class="py-src-variable">File</span>(<span class="py-src-string">"/lost+found"</span>))
106
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">"baz"</span>, <span class="py-src-variable">File</span>(<span class="py-src-string">"/opt"</span>))
108
<span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
109
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">8880</span>, <span class="py-src-variable">factory</span>)
110
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
115
<p><a href="../index.html">Index</a></p>
116
<span class="version">Version: 10.0.0</span>
b'\\ No newline at end of file'