[Twisted-Python] mktap web --resource-script

Tim Allen screwtape at froup.com
Sat May 24 02:16:42 MDT 2003


I've started playing with Twisted Web as a way of trying to purge the 
negative emotions I've associated with server-side scripting due to 
working with ASP every day, but I've hit a bit of a snag.

I could just set up a web.tap file to server ~/public_html, but I 
thought it'd be cool to write the structure of my site in python with 
.putChild() (so I can put all the cool generation code in Python 
modules, rather than parse-every-time .rpy files). The basic script I 
started with looks like this:

	from twisted.web import server, resource as rs

	class IndexPage(rs.Resource):
     	def render(self, request):
         	return "<i>frangipanni</i>"

(I import resource as rs because occasionally I want to refer to 
rs.Resource after defining the magic 'resource' variable, and writing 
'resource.Resource' leads to all manner of pain.)

I made a file like the above called "index.rpy" with the following line 
at the end:

	resource = IndexPage()

and made a tap with:

	mktap web --resource-script=/path/to/index.py

and ran it with:

	twistd --nodaemon -f web.tap

Every time I went to http://localhost:8080/ I got a 404 error ("No Such 
Resource: No Such Child Resource"). Poking around with the IndexPage 
class, I discovered getChild was being called for a child named "", and 
failing.

So I tried making a script to start my website, taking the original 
script and adding:

	#!/usr/bin/env python
	from twisted.internet import app

to the top and:
	
	resource = IndexPage()

	site = server.Site(resource)
	application = app.Application('web')
	application.listenTCP(8080, site)
	application.run()

to the end. Same problem.

After mucking around, I changed the 'resource' line above to be:

	resource = rs.Resource()
	resource.putChild("", IndexPage())

...which worked! I then went back to my index.rpy file to try and use 
it to generate a tap. index.rpy now looks like this:

	from twisted.web import server, resource as rs

	class IndexPage(rs.Resource):
	    def render(self, request):
	        return "<i>frangipanni</i>"
	
	resource = rs.Resource()
	resource.putChild("", IndexPage())

...and still fails in exactly the same way.

I'm using CVS Twisted, and Python 2.2.2 on Mac OS X. What am I missing?





More information about the Twisted-Python mailing list