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/12.1.0/api/twisted.web.error.NoResource.html" title="twisted.web.error.NoResource">NoResource</a></code>, a Twisted Web error resource which
14 responds with a 404 (not found) code. This example will cover the APIs
15 that <code>NoResource</code> uses to do this so that you can generate your own
16 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 (payment
29 required) response. This is really not very different from the resources that
30 was defined in previous examples. The fact that it has a response code other
31 than 200 doesn't change anything else about its role. This will require using
32 the request object, though, which none of the previous examples have done.</p>
34 <p>The <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.server.Request.html" title="twisted.web.server.Request">Request</a></code> object has
35 shown up in a couple of places, but so far we've ignored it. It is a parameter
36 to the <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.resource.Resource.getChild.html" title="twisted.web.resource.Resource.getChild">getChild</a></code>
37 API as well as to render methods such as <code>render_GET</code>. As you might
38 have suspected, it represents the request for which a response is to be
39 generated. Additionally, it also represents the response being generated. In
40 this example we're going to use its <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.web.http.Request.setResponseCode.html" title="twisted.web.http.Request.setResponseCode">setResponseCode</a></code> method to - you guessed
41 it - set the response's status code.</p>
43 <pre class="python"><p class="py-linenumber">1
47 </p><span class="py-src-keyword">class</span> <span class="py-src-identifier">PaymentRequired</span>(<span class="py-src-parameter">Resource</span>):
48 <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>):
49 <span class="py-src-variable">request</span>.<span class="py-src-variable">setResponseCode</span>(<span class="py-src-number">402</span>)
50 <span class="py-src-keyword">return</span> <span class="py-src-string">"<html><body>Please swipe your credit card.</body></html>"</span>
53 <p>Just like the other resources I've demonstrated, this one returns a
54 string from its <code>render_GET</code> method to define the body of
55 the response. All that's different is the call
56 to <code>setResponseCode</code> to override the default response code,
57 200, with a different one.</p>
59 <p>Finally, the code to set up the site and reactor. We'll put an instance of
60 the above defined resource at <code>/buy</code>:</p>
62 <pre class="python"><p class="py-linenumber">1
67 </p><span class="py-src-variable">root</span> = <span class="py-src-variable">Resource</span>()
68 <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>())
69 <span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
70 <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>)
71 <span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
74 <p>Here's the complete example:</p>
76 <pre class="python"><p class="py-linenumber"> 1
90 </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>
91 <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>
92 <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 <span class="py-src-keyword">class</span> <span class="py-src-identifier">PaymentRequired</span>(<span class="py-src-parameter">Resource</span>):
95 <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>):
96 <span class="py-src-variable">request</span>.<span class="py-src-variable">setResponseCode</span>(<span class="py-src-number">402</span>)
97 <span class="py-src-keyword">return</span> <span class="py-src-string">"<html><body>Please swipe your credit card.</body></html>"</span>
99 <span class="py-src-variable">root</span> = <span class="py-src-variable">Resource</span>()
100 <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>())
101 <span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
102 <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>)
103 <span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
106 <p>Run the server and visit <code>http://localhost:8880/buy</code> in your
107 browser. It'll look pretty boring, but if you use Firefox's View Page Info
108 right-click menu item (or your browser's equivalent), you'll be able to see that
109 the server indeed sent back a 402 response code.</p>
113 <p><a href="../index.html">Index</a></p>
114 <span class="version">Version: 12.1.0</span>