~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/web/howto/web-in-60/static-dispatch.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: Static URL Dispatch</title>
 
4
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
 
5
  </head>
 
6
 
 
7
  <body bgcolor="white">
 
8
    <h1 class="title">Static URL Dispatch</h1>
 
9
    <div class="toc"><ol/></div>
 
10
    <div class="content">
 
11
<span/>
 
12
 
 
13
<p>The goal of this example is to show you how to serve different content at
 
14
different URLs.</p>
 
15
 
 
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 &quot;<code>foo</code>&quot; child is retrieved,
 
26
then that resource's &quot;<code>bar</code>&quot; child is retrieved, then that resource
 
27
is used to create the response.</p>
 
28
 
 
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>
 
31
 
 
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>
 
36
 
 
37
<pre class="python"><p class="py-linenumber">1
 
38
2
 
39
3
 
40
4
 
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>
 
45
</pre>
 
46
 
 
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>
 
49
 
 
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>()
 
52
</pre>
 
53
 
 
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>
 
57
 
 
58
<pre class="python"><p class="py-linenumber">1
 
59
2
 
60
3
 
61
</p><span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;foo&quot;</span>, <span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/tmp&quot;</span>))
 
62
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;bar&quot;</span>, <span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/lost+found&quot;</span>))
 
63
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;baz&quot;</span>, <span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/opt&quot;</span>))
 
64
</pre>
 
65
 
 
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>
 
68
 
 
69
<pre class="python"><p class="py-linenumber">1
 
70
2
 
71
3
 
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>()
 
75
</pre>
 
76
 
 
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>
 
82
 
 
83
<p>Here's the whole example uninterrupted:</p>
 
84
 
 
85
<pre class="python"><p class="py-linenumber"> 1
 
86
 2
 
87
 3
 
88
 4
 
89
 5
 
90
 6
 
91
 7
 
92
 8
 
93
 9
 
94
10
 
95
11
 
96
12
 
97
13
 
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>
 
102
 
 
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">&quot;foo&quot;</span>, <span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/tmp&quot;</span>))
 
105
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;bar&quot;</span>, <span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/lost+found&quot;</span>))
 
106
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;baz&quot;</span>, <span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/opt&quot;</span>))
 
107
 
 
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>()
 
111
</pre>
 
112
 
 
113
</div>
 
114
 
 
115
    <p><a href="../index.html">Index</a></p>
 
116
    <span class="version">Version: 10.0.0</span>
 
117
  </body>
 
118
</html>
 
 
b'\\ No newline at end of file'