~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/custom-codes.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: Custom Response Codes</title>
 
4
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
 
5
  </head>
 
6
 
 
7
  <body bgcolor="white">
 
8
    <h1 class="title">Custom Response Codes</h1>
 
9
    <div class="toc"><ol/></div>
 
10
    <div class="content">
 
11
<span/>
 
12
 
 
13
<p>The previous example introduced <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.error.NoResource.html" title="twisted.web.error.NoResource">NoResource</a></code>, a Twisted Web error
 
14
resource which responds with a 404 (not found) code. This example will
 
15
cover the APIs that <code>NoResource</code> uses to do this so that
 
16
you can generate your own custom response codes as desired.</p>
 
17
 
 
18
<p>First, the now-standard import preamble:</p>
 
19
 
 
20
<pre class="python"><p class="py-linenumber">1
 
21
2
 
22
3
 
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
<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>
 
25
<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>
 
26
</pre>
 
27
 
 
28
<p>Now we'll define a new resource class that always returns a 402
 
29
(payment required) response. This is really not very different from
 
30
the resources that was defined in previous examples. The fact that it
 
31
has a response code other than 200 doesn't change anything else about
 
32
its role. This will require using the request object, though, which
 
33
none of the previous examples have done.</p>
 
34
 
 
35
<p>The <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.server.Request.html" title="twisted.web.server.Request">Request</a></code>
 
36
object has shown up in a couple of places, but so far we've ignored
 
37
it. It is a parameter to the <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> API as well as to
 
38
render methods such as <code>render_GET</code>. As you might have
 
39
suspected, it represents the request for which a response is to be
 
40
generated. Additionally, it also represents the response being
 
41
generated. In this example we're going to use its <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.web.http.Request.setResponseCode.html" title="twisted.web.http.Request.setResponseCode">setResponseCode</a></code> method to - you
 
42
guessed it - set the response's status code.</p>
 
43
 
 
44
<pre class="python"><p class="py-linenumber">1
 
45
2
 
46
3
 
47
4
 
48
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">PaymentRequired</span>(<span class="py-src-parameter">Resource</span>):
 
49
    <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>):
 
50
        <span class="py-src-variable">request</span>.<span class="py-src-variable">setResponseCode</span>(<span class="py-src-number">402</span>)
 
51
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;Please swipe your credit card.&quot;</span>
 
52
</pre>
 
53
 
 
54
<p>Just like the other resources I've demonstrated, this one returns a
 
55
string from its <code>render_GET</code> method to define the body of
 
56
the response. All that's different is the call to
 
57
<code>setResponseCode</code> to override the default response code,
 
58
200, with a different one.</p>
 
59
 
 
60
<p>Finally, the code to set up the site and reactor. We'll put an
 
61
instance of the above defined resource at <code>/buy</code>:</p>
 
62
 
 
63
<pre class="python"><p class="py-linenumber">1
 
64
2
 
65
3
 
66
4
 
67
5
 
68
</p><span class="py-src-variable">root</span> = <span class="py-src-variable">Resource</span>()
 
69
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;buy&quot;</span>, <span class="py-src-variable">PaymentRequired</span>())
 
70
<span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
 
71
<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>)
 
72
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
 
73
</pre>
 
74
 
 
75
<p>Here's the complete example:</p>
 
76
 
 
77
<pre class="python"><p class="py-linenumber"> 1
 
78
 2
 
79
 3
 
80
 4
 
81
 5
 
82
 6
 
83
 7
 
84
 8
 
85
 9
 
86
10
 
87
11
 
88
12
 
89
13
 
90
14
 
91
</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>
 
92
<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>
 
93
<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>
 
94
 
 
95
<span class="py-src-keyword">class</span> <span class="py-src-identifier">PaymentRequired</span>(<span class="py-src-parameter">Resource</span>):
 
96
    <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>):
 
97
        <span class="py-src-variable">request</span>.<span class="py-src-variable">setResponseCode</span>(<span class="py-src-number">402</span>)
 
98
        <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;Please swipe your credit card.&quot;</span>
 
99
 
 
100
<span class="py-src-variable">root</span> = <span class="py-src-variable">Resource</span>()
 
101
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">&quot;buy&quot;</span>, <span class="py-src-variable">PaymentRequired</span>())
 
102
<span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
 
103
<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>)
 
104
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
 
105
</pre>
 
106
 
 
107
<p>Run the server and visit <code>http://localhost:8880/buy</code> in
 
108
your browser. It'll look pretty boring, but if you use Firefox's View
 
109
Page Info right-click menu item (or your browser's equivalent), you'll
 
110
be able to see that the server indeed sent back a 402 response
 
111
code.</p>
 
112
 
 
113
</div>
 
114
 
 
115
    <p><a href="../index.html">Index</a></p>
 
116
    <span class="version">Version: 10.0.0</span>
 
117
  </body>
 
118
</html>
 
 
b'\\ No newline at end of file'