diff options
Diffstat (limited to 'doc/web/howto/web-in-60/rpy-scripts.html')
-rw-r--r-- | doc/web/howto/web-in-60/rpy-scripts.html | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/doc/web/howto/web-in-60/rpy-scripts.html b/doc/web/howto/web-in-60/rpy-scripts.html new file mode 100644 index 0000000..7649771 --- /dev/null +++ b/doc/web/howto/web-in-60/rpy-scripts.html @@ -0,0 +1,90 @@ +<?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"> + <head> +<title>Twisted Documentation: rpy scripts (or, how to save yourself some typing)</title> +<link href="../stylesheet.css" rel="stylesheet" type="text/css"/> + </head> + + <body bgcolor="white"> + <h1 class="title">rpy scripts (or, how to save yourself some typing)</h1> + <div class="toc"><ol/></div> + <div class="content"> +<span/> + +<p>The goal of this installment is to show you another way to run a Twisted Web +server with a custom resource which doesn't require as much code as the previous +examples.</p> + +<p>The feature in question is called an <code>rpy script</code>. An rpy script +is a Python source file which defines a resource and can be loaded into a +Twisted Web server. The advantages of this approach are that you don't have to +write code to create the site or set up a listening port with the reactor. That +means fewer lines of code that aren't dedicated to the task you're trying to +accomplish.</p> + +<p>There are some disadvantages, though. An rpy script must have the +extension <code>.rpy</code>. This means you can't import it using the +usual Python import statement. This means it's hard to re-use code in +an rpy script. This also means you can't easily unit test it. The code +in an rpy script is evaluated in an unusual context. So, while rpy +scripts may be useful for testing out ideas, they're not recommend for +much more than that.</p> + +<p>Okay, with that warning out of the way, let's dive in. First, as mentioned, +rpy scripts are Python source files with the <code>.rpy</code> extension. So, +open up an appropriately named file (for example, <code>example.rpy</code>) and +put this code in it:</p> + +<pre class="python"><p class="py-linenumber"> 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +10 +</p><span class="py-src-keyword">import</span> <span class="py-src-variable">time</span> + +<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> + +<span class="py-src-keyword">class</span> <span class="py-src-identifier">ClockPage</span>(<span class="py-src-parameter">Resource</span>): + <span class="py-src-variable">isLeaf</span> = <span class="py-src-variable">True</span> + <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>): + <span class="py-src-keyword">return</span> <span class="py-src-string">"<html><body>%s</body></html>"</span> % (<span class="py-src-variable">time</span>.<span class="py-src-variable">ctime</span>(),) + +<span class="py-src-variable">resource</span> = <span class="py-src-variable">ClockPage</span>() +</pre> + +<p>You may recognize this as the resource from +the <a href="dynamic-content.html" shape="rect">first dynamic rendering +example</a>. What's different is what you don't see: we didn't +import <code>reactor</code> or <code>Site</code>. There are no calls +to <code>listenTCP</code> or <code>run</code>. Instead, and this is +the core idea for rpy scripts, we just bound the +name <code>resource</code> to the resource we want the script to +serve. Every rpy script must bind this name, and this name is the only +thing Twisted Web will pay attention to in an rpy script.</p> + +<p>All that's left is to drop this rpy script into a Twisted Web server. There +are a few ways to do this. The simplest way is with <code>twistd</code>:</p> + +<pre class="shell" xml:space="preserve"> +$ twistd -n web --path . +</pre> + +<p>Hit +<a href="http://localhost:8080/example.rpy" shape="rect">http://localhost:8080/example.rpy</a> +to see it run. You can pass other arguments here too. <code>twistd web</code> +has options for specifying which port number to bind, whether to set up an HTTPS +server, and plenty more. Other options you can pass to <code>twistd</code> allow +you to configure logging to work differently, to select a different reactor, +etc. For a full list of options, see <code>twistd --help</code> and <code>twistd +web --help</code>.</p> + +</div> + + <p><a href="../index.html">Index</a></p> + <span class="version">Version: 12.1.0</span> + </body> +</html>
\ No newline at end of file |