• Antelope Release 5.3 Mac OS X 10.6.8 2013-12-18

 

NAME

rtwebserver_rpy - Serve web content via a python script through rtwebserver

SYNOPSIS


from twisted.web.resource import Resource
from twisted.web import server

class AnExampleResource(Resource):

    def render_GET(self, request):
    ...

resource = AnExampleResource()

DESCRIPTION

The rtwebserver program serves resources of several types to a hierarchy of Uniform Resource Locator (URL) elements as specified in rtwebserver.pf. One of these resource types is the rpy file. The rpy file can conceivably become quite complex, depending on how many capabilities of the twisted.web.resource.Resource framework are exercised. A basic rpy script must start by importing the following two modules:

from twisted.web.resource import Resource
from twisted.web import server

The rpy script must then implement a subclass of Resource that serves the desired content:

class AnExampleResource(Resource):

    def render_GET(self, request):

        request.write("""
            <h1>This works</h1>
        """)

        request.finish()

        return server.NOT_DONE_YET

Finally, the rpy script must end by defining an instance of the class as a variable called resource:

resource = AnExampleResource()

If a restriction is set in the rtwebserver parameter-file valid_ip_addresses table, the request will have the member request.rstrict set to the contents of the restriction string (see legit_ip(3) for further details).

FILES

The resource script must be saved to a filename ending in .rpy.

EXAMPLE

First construct a file anexampleresource.rpy:

% cat anexampleresource.rpy

from twisted.web.resource import Resource
from twisted.web import server

import rtwebserver.config as config

class AnExampleResource(Resource):

    def render_GET(self, request):

        dbname = config.sitedict['siteconfig']['databases']['antelope_demo']

        request.write("<h1>Demo database name is %s</h1>" % dbname)

        request.finish()

        return server.NOT_DONE_YET

resource = AnExampleResource()

%
Then construct an rtwebserver.pf file with the following contents:

% cat rtwebserver.pf
port    8008

site &Arr{
   pythonpaths &Tbl{
   }
   siteconfig &Arr{
        databases &Arr{
                antelope_demo           /opt/antelope/data/db/demo/demo
        }
   }
   pages &Arr{
      index     rpy:anexampleresource.rpy  index
   }
}
%
Then run rtwebserver as follows:

% rtwebserver

The name of the configured database should be available via a web link to the localmachine

http://localhost:8008

SEE ALSO

rtwebserver(1), http://twistedmatrix.com

AUTHOR

Kent Lindquist
Printer icon