TxWeb Alpha – A different spin on twisted.web

I’ve spent some more time on my current pet, txweb, and I think it’s pretty much at the as good as it gets stage.

Below is the source for the example.py

#App level
from txweb import Site, expose
from twisted.web import server, resource
from twisted.internet import reactor
from twisted.web.static import File
from os.path import abspath, dirname, join

Mostly pretty standard imports for a twisted.web application.

Now here is the “Controllers”, they’re stripped down to bare-bones just to keep it simple

class PageOne(object):
    def foo(self, request):
        return "Hello From PageOne Foo!"        
    def delayed(self, request):
        def delayedResponse():
            request.write("I was delayed :( ")
        reactor.callLater(5, delayedResponse)
        return server.NOT_DONE_YET
class PageTwo(object):
    def index(self, request):
        """ /pagetwo/index """
        return "Hello From PageTwo index!"
rootFile = lambda filename : abspath(join(dirname(__file__), filename))
class Root(object):
    def index(self, request):
            Will handle both / and /index paths
        return "Hello From Index!"
    def __default__(self, request):
            Unless overriden further down, this will catch all 404's
        return "I Caught %s " % request.path
    pageone = PageOne()
    pagetwo = PageTwo()
    readme  = File(rootFile("README.md"))
    license = File(rootFile("txweb/LICENSE.txt"))

Basically a txWeb enabled twisted service converts a URL path to an Object path.

So /hello/world could resolve to root.hello.world() if such a construct was provided.

Much more importantly, with the above example, /license resolves to the local file txweb/LICENSE and /readme resolves to README.md !

In summary txweb doesn’t throw away the epic amount of work the Twisted developers and volunteers have put forth, it just presents it in another way.