[Twisted-Python] subclassing twisted.web?

Christopher Armstrong carmstro at twistedmatrix.com
Mon Feb 11 14:57:10 EST 2002

On Mon, 2002-02-11 at 14:09, Kendall Clark wrote:
> Hi,
> I'm playing around with some REST ideas[1], and I want to use this
> approach to build a web-forum-like[2] app for one of my busy content
> sites. I want to use a standalone, Python-based http server for this
> app, so I've been looking at the simple http servers in the Python
> lib, Zope's Z3, Medusa, and twisted.web.
> For various reasons[3], I'd like to try twisted.web first, but I have a
> few qestions --
> (1) Is there a 'servlet' or other programming model to use with
>     twisted.web? If so, is there any example code? How can I get
>     started?

There are two levels of abstraction for web apps once you get past the
low-level stuff: Resources, and WebWidgets, which are built on
Resources. Resources are a 1:1 mapping with Requests (ie, a web request
causes a single Resource to render itself). WebWidgets are more
fine-grained - you can have multiple Widgets on a Page, etc. WebWidgets
is the recommended tool for writing most web apps, and Resources are
mainly a framework thing.

>     In other words, how are people building Web apps with twisted.web?


> (2) I'm also playing around with the idea of 'ad hoc http servers',
>     i.e., lightweight servers that do one thing and one thing well,
>     which gets integrated into an existing site using something like
>     Apache's mod_rewrite and mod_proxy.
>     So if I wanted to build a specialized twisted.web that only did
>     one thing, is there a class I can subclass or methods I can
>     override? 

If you want, you can subclass twisted.protocols.http.HTTP directly, but
I'm not sure if this would be the best way to go for you or not. I'd
need to know more about what you're doing. twisted.web.server.Site
subclasses the base HTTP class to add some more functionality, like
"Request" objects etc, maybe take a look at that too. But like I said
before, most most apps, you probably want to use WebWidgets. If you
think that WebWidgets is too high-level and server.Site is too
low-level, take a look at Resources.

>     In other words, I want to specialize the bit(s) of twisted.web
>     which map REQUEST.URI to a local entity; and I want to do so for
>     particular HTTP methods (specifically, GET, POST, DELETE) and
>     return the appropriate non-implemented HTTP response for
>     everything else (i.e., 404 for non-existent URIs and <whatever>
>     for the other HTTP methods).

Yeah, take a look at twisted.protocols.http.HTTP and

Any more questions, feel free to respond or ask me on IRC.

                                Chris Armstrong
                         << radix at twistedmatrix.com >>

More information about the Twisted-Python mailing list