:LastChangedDate: $LastChangedDate$ :LastChangedRevision: $LastChangedRevision$ :LastChangedBy: $LastChangedBy$ rpy scripts (or, how to save yourself some typing) ================================================== 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. The feature in question is called an ``rpy script`` . 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. There are some disadvantages, though. An rpy script must have the extension ``.rpy`` . 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. Okay, with that warning out of the way, let's dive in. First, as mentioned, rpy scripts are Python source files with the ``.rpy`` extension. So, open up an appropriately named file (for example, ``example.rpy`` ) and put this code in it: .. code-block:: python import time from twisted.web.resource import Resource class ClockPage(Resource): isLeaf = True def render_GET(self, request): return (b"
" b"