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: Generating a Page Dynamically</title>
4
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
8
<h1 class="title">Generating a Page Dynamically</h1>
9
<div class="toc"><ol/></div>
13
<p>The goal of this example is to show you how to dynamically generate the
14
contents of a page.</p>
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>
18
<pre class="python"><p class="py-linenumber">1
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>
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>
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>
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>
36
<p>Since we're going to make the demo resource a clock, we'll also import the
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>
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>
47
<pre class="python"><p class="py-linenumber">1
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">"%s"</span> % (<span class="py-src-variable">time</span>.<span class="py-src-variable">ctime</span>(),)
57
<p>Setting <code>isLeaf</code> to <code>True</code> indicates that
58
<code>ClockPage</code> resources will never have any children.</p>
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>
64
<p>With the resource defined, we can create a <code>Site</code> from it:</p>
66
<pre class="python"><p class="py-linenumber">1
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>)
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>
77
<pre class="python"><p class="py-linenumber">1
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>()
83
<p>Here's the code with no interruptions:</p>
85
<pre class="python"><p class="py-linenumber"> 1
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>
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">"%s"</span> % (<span class="py-src-variable">time</span>.<span class="py-src-variable">ctime</span>(),)
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>()
117
<p><a href="../index.html">Index</a></p>
118
<span class="version">Version: 10.0.0</span>
b'\\ No newline at end of file'