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: rpy scripts (or, how to save yourself some typing)</title>
4
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
8
<h1 class="title">rpy scripts (or, how to save yourself some typing)</h1>
9
<div class="toc"><ol/></div>
13
<p>The goal of this installment is to show you another way to run a Twisted Web
14
server with a custom resource which doesn't require as much code as the previous
17
<p>The feature in question is called an <code>rpy script</code>. An rpy script
18
is a Python source file which defines a resource and can be loaded into a
19
Twisted Web server. The advantages of this approach are that you don't have to
20
write code to create the site or set up a listening port with the reactor. That
21
means fewer lines of code that aren't dedicated to the task you're trying to
24
<p>There are some disadvantages, though. An rpy script must have the extension
25
<code>.rpy</code>. This means you can't import it using the usual Python import
26
statement. This means it's hard to re-use code in an rpy script. This also means
27
you can't easily unit test it. The code in an rpy script is evaluated in an
28
unusual context. So, while rpy scripts may be useful for testing out ideas,
29
they're not recommend for much more than that.</p>
31
<p>Okay, with that warning out of the way, let's dive in. First, as mentioned,
32
rpy scripts are Python source files with the <code>.rpy</code> extension. So,
33
open up an appropriately named file (for example, <code>example.rpy</code>) and
34
put this code in it:</p>
36
<pre class="python"><p class="py-linenumber"> 1
46
</p><span class="py-src-keyword">import</span> <span class="py-src-variable">time</span>
48
<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>
50
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ClockPage</span>(<span class="py-src-parameter">Resource</span>):
51
<span class="py-src-variable">isLeaf</span> = <span class="py-src-variable">True</span>
52
<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>):
53
<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>(),)
55
<span class="py-src-variable">resource</span> = <span class="py-src-variable">ClockPage</span>()
58
<p>You may recognize this as the resource from the <a href="dynamic-content.html" shape="rect">first dynamic rendering example</a>. What's
59
different is what you don't see: we didn't import <code>reactor</code> or
60
<code>Site</code>. There are no calls to <code>listenTCP</code> or
61
<code>run</code>. Instead, and this is the core idea for rpy scripts, we just
62
bound the name <code>resource</code> to the resource we want the script to
63
serve. Every rpy script must bind this name, and this name is the only thing
64
Twisted Web will pay attention to in an rpy script.</p>
66
<p>All that's left is to drop this rpy script into a Twisted Web server. There
67
are a few ways to do this. The simplest way is with <code>twistd</code>:</p>
69
<pre class="shell" xml:space="preserve">
70
$ twistd -n web --path .
73
<p>Hit <a href="http://localhost:8080/example.rpy" shape="rect">http://localhost:8080/example.rpy</a>
74
to see it run. You can pass other arguments here too. <code>twistd web</code>
75
has options for specifying which port number to bind, whether to set up an HTTPS
76
server, and plenty more. Other options you can pass to <code>twistd</code> allow
77
you to configure logging to work differently, to select a different reactor,
78
etc. For a full list of options, see <code>twistd --help</code> and <code>twistd
79
web --help</code>.</p>
83
<p><a href="../index.html">Index</a></p>
84
<span class="version">Version: 10.0.0</span>
b'\\ No newline at end of file'