[Twisted-Python] Getting the hang of twisted.web
Micah Yoder
micahlists at comcast.net
Sun Aug 3 00:38:06 MDT 2003
Ok, I have the following in web.py:
***
from twisted.web import resource
class Simple(resource.Resource):
isLeaf = True
def render(self, request):
return "<html>Hello, world!</html>"
class Hello(resource.Resource):
def __init__(self):
resource.Resource.__init__(self)
self.putChild('simple', Simple())
def getChild(self, name, request):
if name == '':
return self
return Resource.getChild(
self, name, request)
def render(self, request):
s = request.getSession()
if not hasattr(s, 'var'):
s.var = 1
else:
s.var = s.var * 2
return """<html>
Hello, world! I am located at %r and am at %d. My args are %s. Site %s.
</html>""" % (request.prepath, s.var, repr(request.args),
repr(request.site))
***
Then I:
$ mktap web --class=web.Hello
$ twistd -f web.tap
Seems to work fine. I can go to http://localhost:8080/ to get the Hello()
class and http://localhost:8080/simple to get the Simple() class.
Questions:
1. Is there anything I'm doing obviously wrong or a "non-Twisted" way, like
with the session variable or the child stuff?
2. What about persistant HTTP sessions? I thought one of the points of this
mktap stuff was to allow persistance in protocols. I do:
$ killall twistd
$ twistd -f web-shutdown.tap
and go to the index page again and the counter gets re-started at 1. Can't
they be made to persist?
3. Finally, some advice on how to structure my "real" project would be
appreciated. I have a program that will store and index a large amount of
data (several megabytes), and intend to have resources that search and
display it. The original importing of the data takes several seconds now,
and will take probably a minute or more by the time I'm done. The data is
being stored in a Python class, with methods to set it up and search it,
along with direct public instance-variable access to some structures.
The question is, where should this class object be stored in a .tap-ified
program? Right now I have the class instance as a global variable in the
module that my related resource objects are in, but that doesn't seem ideal.
I thought about storing it in the Site() object, but there doesn't seem to be
a way to access the Site object from a resource request. request.site only
returns None ... is ths a bug?
(Yes, I've been doing this application so far with just a reactor and a Site()
object. But it seems prudent to try to .tap-ify it.)
Thanks!
Micah
--
Yoder Internet Development
Honest and Affordable Web Development and Linux consulting
http://yoderdev.com
More information about the Twisted-Python
mailing list