Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / web / howto / web-in-60 / rpy-scripts.html
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: rpy scripts (or, how to save yourself some typing)</title>
4 <link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
5   </head>
6
7   <body bgcolor="white">
8     <h1 class="title">rpy scripts (or, how to save yourself some typing)</h1>
9     <div class="toc"><ol/></div>
10     <div class="content">
11 <span/>
12
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
15 examples.</p>
16
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
22 accomplish.</p>
23
24 <p>There are some disadvantages, though. An rpy script must have the
25 extension <code>.rpy</code>. This means you can't import it using the
26 usual Python import statement. This means it's hard to re-use code in
27 an rpy script. This also means you can't easily unit test it. The code
28 in an rpy script is evaluated in an unusual context. So, while rpy
29 scripts may be useful for testing out ideas, they're not recommend for
30 much more than that.</p>
31
32 <p>Okay, with that warning out of the way, let's dive in. First, as mentioned,
33 rpy scripts are Python source files with the <code>.rpy</code> extension. So,
34 open up an appropriately named file (for example, <code>example.rpy</code>) and
35 put this code in it:</p>
36
37 <pre class="python"><p class="py-linenumber"> 1
38  2
39  3
40  4
41  5
42  6
43  7
44  8
45  9
46 10
47 </p><span class="py-src-keyword">import</span> <span class="py-src-variable">time</span>
48
49 <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
51 <span class="py-src-keyword">class</span> <span class="py-src-identifier">ClockPage</span>(<span class="py-src-parameter">Resource</span>):
52     <span class="py-src-variable">isLeaf</span> = <span class="py-src-variable">True</span>
53     <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>):
54         <span class="py-src-keyword">return</span> <span class="py-src-string">&quot;&lt;html&gt;&lt;body&gt;%s&lt;/body&gt;&lt;/html&gt;&quot;</span> % (<span class="py-src-variable">time</span>.<span class="py-src-variable">ctime</span>(),)
55
56 <span class="py-src-variable">resource</span> = <span class="py-src-variable">ClockPage</span>()
57 </pre>
58
59 <p>You may recognize this as the resource from
60 the <a href="dynamic-content.html" shape="rect">first dynamic rendering
61 example</a>. What's different is what you don't see: we didn't
62 import <code>reactor</code> or <code>Site</code>. There are no calls
63 to <code>listenTCP</code> or <code>run</code>. Instead, and this is
64 the core idea for rpy scripts, we just bound the
65 name <code>resource</code> to the resource we want the script to
66 serve. Every rpy script must bind this name, and this name is the only
67 thing Twisted Web will pay attention to in an rpy script.</p>
68
69 <p>All that's left is to drop this rpy script into a Twisted Web server. There
70 are a few ways to do this. The simplest way is with <code>twistd</code>:</p>
71
72 <pre class="shell" xml:space="preserve">
73 $ twistd -n web --path .
74 </pre>
75
76 <p>Hit 
77 <a href="http://localhost:8080/example.rpy" shape="rect">http://localhost:8080/example.rpy</a>
78 to see it run. You can pass other arguments here too. <code>twistd web</code>
79 has options for specifying which port number to bind, whether to set up an HTTPS
80 server, and plenty more. Other options you can pass to <code>twistd</code> allow
81 you to configure logging to work differently, to select a different reactor,
82 etc. For a full list of options, see <code>twistd --help</code> and <code>twistd
83 web --help</code>.</p>
84
85 </div>
86
87     <p><a href="../index.html">Index</a></p>
88     <span class="version">Version: 12.1.0</span>
89   </body>
90 </html>