[Twisted-web] Writing a resource like PythonScript
Donnie Hale
dhale at speakeasy.net
Fri Jul 16 23:15:55 MDT 2004
I'm trying to write a resource class like the
twisted.web.script.PythonScript class. As an example, I've got this in a
file named TestResource.py:
from twisted.web.resource import Resource
class TestScript(resource.Resource):
isLeaf = 1
def __init__(self, filename, registry):
self.filename = filename
self.registry = registry
def render(self, request):
request.setHeader("x-powered-by","Twisted/%s" % copyright.version)
request.write("We've arrived in TestScript.render")
request.finish()
return server.NOT_DONE_YET
However, when I go to create a .tap file using the command:
mktap web --path=webroot --processor=".cht=TestResource.TestScript"
--port=8880
I get the error:
NameError: name 'resource' is not defined
Now I've seen that almost all (if not all) the examples of deriving from
Resource have a line like "resource = MyResource()", the PythonScript class
is conspicuous in that it doesn't have such a line, and in fact is in the
middle of a file with other Resource classes where trying to assign some
class instance to a variable named "resource" would be ambiguous. And in
fact I've grepped the source of the twisted.web directory structure and
can't find anyplace a PythonScript is explicitly instantiated. But I know it
works, as I've created a .epy file which does what I'd expect in a basic
web.tap application.
So my question is, how can I create a class like the PythonScript class,
which doesn't explicitly declare a "resource" variable, can get associated
with a file extension via the "mktap --processor" command, and then behaves
intuitively at runtime?
Many thanks,
Donnie
P.S. Somewhere in the docs there should be a comment that if you use the
--processor flag for mktap on windows, you need to surround the
"ext=processor" part of the parameters with double-quotes (see above). If
you don't do that, mktap will complain about the options not having the
right number of values to unpack. That took a while to figure out.
More information about the Twisted-web
mailing list