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: Custom Response Codes</title>
4
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
8
<h1 class="title">Custom Response Codes</h1>
9
<div class="toc"><ol/></div>
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>
18
<p>First, the now-standard import preamble:</p>
20
<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
<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>
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>
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>
44
<pre class="python"><p class="py-linenumber">1
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">"Please swipe your credit card."</span>
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>
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>
63
<pre class="python"><p class="py-linenumber">1
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">"buy"</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>()
75
<p>Here's the complete example:</p>
77
<pre class="python"><p class="py-linenumber"> 1
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>
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">"Please swipe your credit card."</span>
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">"buy"</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>()
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
115
<p><a href="../index.html">Index</a></p>
116
<span class="version">Version: 10.0.0</span>
b'\\ No newline at end of file'