~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-content.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: Serving Static Content From a Directory</title>
 
4
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
 
5
  </head>
 
6
 
 
7
  <body bgcolor="white">
 
8
    <h1 class="title">Serving Static Content From a Directory</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 static content
 
14
from a filesystem. First, we need to import some objects:</p>
 
15
 
 
16
<ul>
 
17
 
 
18
<li>
 
19
<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>, an <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.internet.interfaces.IProtocolFactory.html" title="twisted.internet.interfaces.IProtocolFactory">IProtocolFactory</a></code> which
 
20
glues a listening server port (<code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.internet.interfaces.IListeningPort.html" title="twisted.internet.interfaces.IListeningPort">IListeningPort</a></code>) to the <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.http.HTTPChannel.html" title="twisted.web.http.HTTPChannel">HTTPChannel</a></code>
 
21
implementation:
 
22
<pre class="python"><p class="py-linenumber">1
 
23
</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>
 
24
</pre>
 
25
</li>
 
26
 
 
27
<li>
 
28
<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>, an <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> which glues
 
29
the HTTP protocol implementation to the filesystem:
 
30
<pre class="python"><p class="py-linenumber">1
 
31
</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">static</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">File</span>
 
32
</pre>
 
33
</li>
 
34
 
 
35
<li>
 
36
The <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.internet.reactor.html" title="twisted.internet.reactor">reactor</a></code>, which
 
37
drives the whole process, actually accepting TCP connections and
 
38
moving bytes into and out of them:
 
39
<pre class="python"><p class="py-linenumber">1
 
40
</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>
 
41
</pre>
 
42
</li>
 
43
 
 
44
</ul>
 
45
 
 
46
Next, we create an instance of the File resource pointed at the
 
47
directory to serve:
 
48
<pre class="python"><p class="py-linenumber">1
 
49
</p><span class="py-src-variable">resource</span> = <span class="py-src-variable">File</span>(<span class="py-src-string">&quot;/tmp&quot;</span>)
 
50
</pre>
 
51
 
 
52
Then we create an instance of the Site factory with that resource:
 
53
<pre class="python"><p class="py-linenumber">1
 
54
</p><span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">resource</span>)
 
55
</pre>
 
56
 
 
57
Now we glue that factory to a TCP port:
 
58
<pre class="python"><p class="py-linenumber">1
 
59
</p><span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">8888</span>, <span class="py-src-variable">factory</span>)
 
60
</pre>
 
61
 
 
62
Finally, we start the reactor so it can make the program work:
 
63
<pre class="python"><p class="py-linenumber">1
 
64
</p><span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
 
65
</pre>
 
66
And that's it. Here's the complete program:
 
67
 
 
68
<pre class="python"><p class="py-linenumber">1
 
69
2
 
70
3
 
71
4
 
72
5
 
73
6
 
74
7
 
75
8
 
76
</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>
 
77
<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>
 
78
<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>
 
79
 
 
80
<span class="py-src-variable">resource</span> = <span class="py-src-variable">File</span>(<span class="py-src-string">'/tmp'</span>)
 
81
<span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">resource</span>)
 
82
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">8888</span>, <span class="py-src-variable">factory</span>)
 
83
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
 
84
</pre>
 
85
 
 
86
<p>Bonus example! For those times when you don't actually want to
 
87
write a new program, the above implemented functionality is one of the
 
88
things the command line <code>twistd</code> tool can do. In this case,
 
89
the command
 
90
<pre xml:space="preserve">
 
91
twistd -n web --path /tmp
 
92
</pre>
 
93
will accomplish the same thing as the above server. See <a href="../../../core/howto/basics.html" shape="rect">helper programs</a> in the
 
94
Twisted Core documentation for more information on using
 
95
<code>twistd</code>.</p>
 
96
 
 
97
</div>
 
98
 
 
99
    <p><a href="../index.html">Index</a></p>
 
100
    <span class="version">Version: 10.0.0</span>
 
101
  </body>
 
102
</html>
 
 
b'\\ No newline at end of file'