This file is indexed.

/usr/share/doc/python-twisted-web/howto/web-in-60/static-dispatch.html is in python-twisted-web 13.2.0-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?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">
  <head>
<title>Twisted Documentation: Static URL Dispatch</title>
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
  </head>

  <body bgcolor="white">
    <h1 class="title">Static URL Dispatch</h1>
    <div class="toc"><ol/></div>
    <div class="content">
<span/>

<p>The goal of this example is to show you how to serve different content at
different URLs.</p>

<p>The key to understanding how different URLs are handled with the resource
APIs in Twisted Web is understanding that any URL can be used to address a node
in a tree. Resources in Twisted Web exist in such a tree, and a request for a
URL will be responded to by the resource which that URL addresses. The
addressing scheme considers only the path segments of the URL. Starting with the
root resource (the one used to construct the <code>Site</code>) and the first
path segment, a child resource is looked up. As long as there are more path
segments, this process is repeated using the result of the previous lookup and
the next path segment. For example, to handle a request
for <code>&quot;/foo/bar&quot;</code>, first the root's <code>&quot;foo&quot;</code> child is
retrieved, then that resource's <code>&quot;bar&quot;</code> child is retrieved, then that
resource is used to create the response.</p>

<p>With that out of the way, let's consider an example that can serve a few
different resources at a few different URLs.</p>

<p>First things first: we need to import <code class="API"><a href="http://twistedmatrix.com/documents/13.2.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/13.2.0/api/twisted.web.resource.Resource.html" title="twisted.web.resource.Resource">Resource</a></code>, a convenient base class
for custom pages, and <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.internet.reactor.html" title="twisted.internet.reactor">reactor</a></code>,
the object which implements the Twisted main loop. We'll also import <code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.static.File.html" title="twisted.web.static.File">File</a></code> to use as the resource at one
of the example URLs.</p>

<pre class="python"><p class="py-linenumber">1
2
3
4
</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>
<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>
<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>
<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>
</pre>

<p>Now we create a resource which will correspond to the root of the URL
hierarchy: all URLs are children of this resource.</p>

<pre class="python"><p class="py-linenumber">1
</p><span class="py-src-variable">root</span> = <span class="py-src-variable">Resource</span>()
</pre>

<p>Here comes the interesting part of this example. We're now going to
create three more resources and attach them to the three
URLs <code>/foo</code>, <code>/bar</code>, and <code>/baz</code>:</p>

<pre class="python"><p class="py-linenumber">1
2
3
</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>))
<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>))
<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>))
</pre>

<p>Last, all that's required is to create a <code>Site</code> with the root
resource, associate it with a listening server port, and start the reactor:</p>

<pre class="python"><p class="py-linenumber">1
2
3
</p><span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
<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>)
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
</pre>

<p>With this server running, <code>http://localhost:8880/foo</code>
will serve a listing of files
from <code>/tmp</code>, <code>http://localhost:8880/bar</code> will
serve a listing of files from <code>/lost+found</code>,
and <code>http://localhost:8880/baz</code> will serve a listing of
files from <code>/opt</code>.</p>

<p>Here's the whole example uninterrupted:</p>

<pre class="python"><p class="py-linenumber"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
</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>
<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>
<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>
<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>

<span class="py-src-variable">root</span> = <span class="py-src-variable">Resource</span>()
<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>))
<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>))
<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>))

<span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
<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>)
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
</pre>

</div>

    <p><a href="../index.html">Index</a></p>
    <span class="version">Version: 13.2.0</span>
  </body>
</html>