[Twisted-web] Trac via twisted.web2.wsgi

Ed Suominen general at eepatents.com
Thu Mar 16 04:23:50 CST 2006

I've implemented a twisted.web2 resource for accessing Trac via WSGI.
You can see both a working example and the code at


There's a StanResource class there that I'm using and think is kind of
cool, too.

I use the following python code to provide my root resource for the
foss.eepatents.com site. (A copy is at http://deadbeefbabe.org/paste/34
in case the email version is too mangled to be useful.) Obviously,
you'll want to personalize it for your own use and not call the home
page "Ed Suominen's Free & Open Source Software Projects."

This will all be part of an upcoming release of DynamicSite, a flexible,
reasonably user-friendly vhost implementation of twisted.web2. Hopefully
I will remain inspired to package it all up now that I've scratched my
particular itch. Since I believe in generous usage of docstrings (hint,
hint to the Twisted devs), the code in the kiss-web2 branch should be
fairly easy to use even in its current raw form.

Those interested in what can be (over)done with Nevow, dynamic image
generation, intelligent caching, etc. might want to browse through the
current revision (SVN:47) of trunk before I replace it with the "keep it
simple stupid" twisted.web2 branch.

Best regards,
Ed Suominen

> # DynamicSite:
> # A virtual hosting twisted.web2 HTTP server that uses subdirectories for
> # virtual host content. Subdirectories can be python packages providing dynamic
> # content with a root resource object, or sources of static content.
> #
> # Copyright (C) 2006 by Edwin A. Suominen, http://www.eepatents.com
> #
> # This program is free software; you can redistribute it and/or modify it under
> # the terms of the GNU General Public License as published by the Free Software
> # Foundation; either version 2 of the License, or (at your option) any later
> # version.
> # 
> # This program is distributed in the hope that it will be useful, but WITHOUT
> # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
> # FOR A PARTICULAR PURPOSE.  See the file COPYING for more details.
> # 
> # You should have received a copy of the GNU General Public License along with
> # this program; if not, write to the Free Software Foundation, Inc., 51
> # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
> """
> Provides a Trac resource via an WSGI gateway.
> """
> from dynamicsite.external import TracResource, StanResource
> class HomePage(StanResource):
>     """
>     A home page for all of the trac project names and paths supplied in the
>     I{projects} dict
>     """
>     def __init__(self, projects):
>         self.projects = projects
>     def render(self, request):
>         title = "Ed Suominen's Free & Open Source Software Projects"
>         body = [self.h2[title]]
>         for projectName in self.projects.iterkeys():
>             projectDescription = " ".join(self.projects[projectName][1:])
>             body.append(self.p[self.a(href=projectName)[projectName],
>                                ": %s" % projectDescription])
>         return [self.html[
>                 self.head[self.title[title]],
>                 self.body[body]]]
>     def locateChild(self, request, segments):
>         projectName = segments[0]
>         if projectName in self.projects:
>             projectPath = self.projects[projectName][0]
>             return TracResource(projectPath), segments[1:]
>         else:
>             return self, ()
> projects = {"pNetworkX":("/var/trac/pnetworkx",
>                          "An enhancement to the NetworkX package that provides",
>                          "database persistency of underlying data in graph",
>                          "objects"),
>             "DynamicSite":("/var/trac/dynamicsite",
>                            "A virtual hosting twisted.web2 HTTP server that",
>                            "uses subdirectories for virtual host content")
>             }
> resource = HomePage(projects)

More information about the Twisted-web mailing list