~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/dynamic-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: Generating a Page Dynamically</title>
 
4
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
 
5
  </head>
 
6
 
 
7
  <body bgcolor="white">
 
8
    <h1 class="title">Generating a Page Dynamically</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 dynamically generate the
 
14
contents of a page.</p>
 
15
 
 
16
<p>Taking care of some of the necessary imports first, we'll 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> and 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>:</p>
 
17
 
 
18
<pre class="python"><p class="py-linenumber">1
 
19
2
 
20
</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>
 
21
<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>
 
22
</pre>
 
23
 
 
24
<p>The Site is a factory which associates a listening port with the HTTP
 
25
protocol implementation. The reactor is the main loop that drives any Twisted
 
26
application; we'll use it to actually create the listening port in a moment.</p>
 
27
 
 
28
<p>Next, we'll import one more thing from Twisted Web: <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>. An instance of
 
29
<code>Resource</code> (or a subclass) represents a page (technically, the entity
 
30
addressed by a URI).</p>
 
31
 
 
32
<pre class="python"><p class="py-linenumber">1
 
33
</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>
 
34
</pre>
 
35
 
 
36
<p>Since we're going to make the demo resource a clock, we'll also import the
 
37
time module:</p>
 
38
 
 
39
<pre class="python"><p class="py-linenumber">1
 
40
</p><span class="py-src-keyword">import</span> <span class="py-src-variable">time</span>
 
41
</pre>
 
42
 
 
43
<p>With imports taken care of, the next step is to define a
 
44
<code>Resource</code> subclass which has the dynamic rendering behavior we
 
45
want. Here's a resource which generates a page giving the time:</p>
 
46
 
 
47
<pre class="python"><p class="py-linenumber">1
 
48
2
 
49
3
 
50
4
 
51
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">ClockPage</span>(<span class="py-src-parameter">Resource</span>):
 
52
    <span class="py-src-variable">isLeaf</span> = <span class="py-src-variable">True</span>
 
53
    <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>):
 
54
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;%s&quot;</span> % (<span class="py-src-variable">time</span>.<span class="py-src-variable">ctime</span>(),)
 
55
</pre>
 
56
 
 
57
<p>Setting <code>isLeaf</code> to <code>True</code> indicates that
 
58
<code>ClockPage</code> resources will never have any children.</p>
 
59
 
 
60
<p>The <code>render_GET</code> method here will be called whenever the URI we
 
61
hook this resource up to is requested with the <code>GET</code> method. The byte
 
62
string it returns is what will be sent to the browser.</p>
 
63
 
 
64
<p>With the resource defined, we can create a <code>Site</code> from it:</p>
 
65
 
 
66
<pre class="python"><p class="py-linenumber">1
 
67
2
 
68
</p><span class="py-src-variable">resource</span> = <span class="py-src-variable">ClockPage</span>()
 
69
<span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">resource</span>)
 
70
</pre>
 
71
 
 
72
<p>Just as with the previous static content example, this configuration puts our
 
73
resource at the very top of the URI hierarchy, ie at <code>/</code>. With that
 
74
<code>Site</code> instance, we can tell the reactor to <a href="../../../core/howto/servers.html" shape="rect">create a TCP server</a> and start
 
75
servicing requests:</p>
 
76
 
 
77
<pre class="python"><p class="py-linenumber">1
 
78
2
 
79
</p><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>)
 
80
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
 
81
</pre>
 
82
 
 
83
<p>Here's the code with no interruptions:</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
14
 
99
</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>
 
100
<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>
 
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">resource</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Resource</span>
 
102
<span class="py-src-keyword">import</span> <span class="py-src-variable">time</span>
 
103
 
 
104
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ClockPage</span>(<span class="py-src-parameter">Resource</span>):
 
105
    <span class="py-src-variable">isLeaf</span> = <span class="py-src-variable">True</span>
 
106
    <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>):
 
107
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;%s&quot;</span> % (<span class="py-src-variable">time</span>.<span class="py-src-variable">ctime</span>(),)
 
108
 
 
109
<span class="py-src-variable">resource</span> = <span class="py-src-variable">ClockPage</span>()
 
110
<span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">resource</span>)
 
111
<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>)
 
112
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
 
113
</pre>
 
114
 
 
115
</div>
 
116
 
 
117
    <p><a href="../index.html">Index</a></p>
 
118
    <span class="version">Version: 10.0.0</span>
 
119
  </body>
 
120
</html>
 
 
b'\\ No newline at end of file'