~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-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: Dynamic URL Dispatch</title>
 
4
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
 
5
  </head>
 
6
 
 
7
  <body bgcolor="white">
 
8
    <h1 class="title">Dynamic URL Dispatch</h1>
 
9
    <div class="toc"><ol/></div>
 
10
    <div class="content">
 
11
<span/>
 
12
 
 
13
<p>In the <a href="static-dispatch.html" shape="rect">previous example</a> we covered how to
 
14
statically configure Twisted Web to serve different content at different
 
15
URLs. The goal of this example is to show you how to do this dynamically
 
16
instead. Reading the previous installment if you haven't already is suggested in
 
17
order to get an overview of how URLs are treated when using Twisted Web's <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.resource.html" title="twisted.web.resource">resource</a></code> APIs.</p>
 
18
 
 
19
<p><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 object which
 
20
associates a listening server port with the HTTP implementation), <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
 
21
to use when defining 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> (the object which implements the Twisted
 
22
main loop) return once again:</p>
 
23
 
 
24
<pre class="python"><p class="py-linenumber">1
 
25
2
 
26
3
 
27
</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>
 
28
<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>
 
29
<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>
 
30
</pre>
 
31
 
 
32
<p>With that out of the way, here's the interesting part of this example. We're
 
33
going to define a resource which renders a whole-year calendar. The year it will
 
34
render the calendar for will be the year in the request URL. So, for example,
 
35
<code>/2009</code> will render a calendar for 2009. First, here's a resource
 
36
that renders a calendar for the year passed to its initializer:</p>
 
37
 
 
38
<pre class="python"><p class="py-linenumber">1
 
39
2
 
40
3
 
41
4
 
42
5
 
43
6
 
44
7
 
45
8
 
46
9
 
47
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">calendar</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">calendar</span>
 
48
 
 
49
<span class="py-src-keyword">class</span> <span class="py-src-identifier">YearPage</span>(<span class="py-src-parameter">Resource</span>):
 
50
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">year</span>):
 
51
        <span class="py-src-variable">Resource</span>.<span class="py-src-variable">__init__</span>(<span class="py-src-variable">self</span>)
 
52
        <span class="py-src-variable">self</span>.<span class="py-src-variable">year</span> = <span class="py-src-variable">year</span>
 
53
 
 
54
    <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>):
 
55
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;%s&quot;</span> % (<span class="py-src-variable">calendar</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">year</span>),)
 
56
</pre>
 
57
 
 
58
<p>Pretty simple - not all that different from the first dynamic resource
 
59
demonstrated in <a href="dynamic-content.html" shape="rect">Generating a Page
 
60
Dynamically</a>. Now here's the resource that handles URLs with a year in them
 
61
by creating a suitable instance of this <code>YearPage</code> class:</p>
 
62
 
 
63
<pre class="python"><p class="py-linenumber">1
 
64
2
 
65
3
 
66
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">Calendar</span>(<span class="py-src-parameter">Resource</span>):
 
67
  <span class="py-src-keyword">def</span> <span class="py-src-identifier">getChild</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">name</span>, <span class="py-src-parameter">request</span>):
 
68
      <span class="py-src-keyword">return</span> <span class="py-src-variable">YearPage</span>(<span class="py-src-variable">int</span>(<span class="py-src-variable">name</span>))
 
69
</pre>
 
70
 
 
71
<p>By implementing <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.resource.Resource.getChild.html" title="twisted.web.resource.Resource.getChild">getChild</a></code> here, we've just defined
 
72
how Twisted Web should find children of <code>Calendar</code> instances when
 
73
it's resolving an URL into a resource. This implementation defines all integers
 
74
as the children of <code>Calendar</code> (and punts on error handling, more on
 
75
that later).</p>
 
76
 
 
77
<p>All that's left is to create a <code>Site</code> using this resource as its
 
78
root and then start the reactor:</p>
 
79
 
 
80
<pre xml:space="preserve">
 
81
root = Calendar()
 
82
factory = Site(root)
 
83
reactor.listenTCP(8880, factory)
 
84
reactor.run()
 
85
</pre>
 
86
 
 
87
<p>And that's all. Any resource-based dynamic URL handling is going to look
 
88
basically like <code>Calendar.getPage</code>. Here's the full example code:</p>
 
89
 
 
90
<pre class="python"><p class="py-linenumber"> 1
 
91
 2
 
92
 3
 
93
 4
 
94
 5
 
95
 6
 
96
 7
 
97
 8
 
98
 9
 
99
10
 
100
11
 
101
12
 
102
13
 
103
14
 
104
15
 
105
16
 
106
17
 
107
18
 
108
19
 
109
20
 
110
21
 
111
22
 
112
</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>
 
113
<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>
 
114
<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>
 
115
 
 
116
<span class="py-src-keyword">from</span> <span class="py-src-variable">calendar</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">calendar</span>
 
117
 
 
118
<span class="py-src-keyword">class</span> <span class="py-src-identifier">YearPage</span>(<span class="py-src-parameter">Resource</span>):
 
119
    <span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">year</span>):
 
120
        <span class="py-src-variable">Resource</span>.<span class="py-src-variable">__init__</span>(<span class="py-src-variable">self</span>)
 
121
        <span class="py-src-variable">self</span>.<span class="py-src-variable">year</span> = <span class="py-src-variable">year</span>
 
122
 
 
123
    <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>):
 
124
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;%s&quot;</span> % (<span class="py-src-variable">calendar</span>(<span class="py-src-variable">self</span>.<span class="py-src-variable">year</span>),)
 
125
 
 
126
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Calendar</span>(<span class="py-src-parameter">Resource</span>):
 
127
  <span class="py-src-keyword">def</span> <span class="py-src-identifier">getChild</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">name</span>, <span class="py-src-parameter">request</span>):
 
128
      <span class="py-src-keyword">return</span> <span class="py-src-variable">YearPage</span>(<span class="py-src-variable">int</span>(<span class="py-src-variable">name</span>))
 
129
 
 
130
<span class="py-src-variable">root</span> = <span class="py-src-variable">Calendar</span>()
 
131
<span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
 
132
<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>)
 
133
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
 
134
</pre>
 
135
 
 
136
 
 
137
</div>
 
138
 
 
139
    <p><a href="../index.html">Index</a></p>
 
140
    <span class="version">Version: 10.0.0</span>
 
141
  </body>
 
142
</html>
 
 
b'\\ No newline at end of file'