|Version 7 (modified by exarkun, 7 years ago) (diff)|
Web Development with Twisted
Twisted has a long and checkered past in web development. This page is a roadmap to the wilderness that the landscape of web development with Twisted has become.
What's Going On Around Here
There are a number of packages in and around Twisted that do things with the web:
- Twisted Web provides a simple, stable resource publishing API, on top of an HTTP/1.0 server implementation with some HTTP/1.1 features. Twisted Web is widely regarded as clunky and old, but extremely stable.
- Several deprecated templating toolkits included with twisted.web, such as "woven" and "twisted web widgets". Don't use these! They're only there for compatibility.
- Twisted Web 2 is a complete re-write of Twisted Web, with a new HTTP/1.1 implementation and a new resource publishing model. Development focus has shifted to porting the best parts of Twisted Web 2 back to Twisted Web where existing applications will benefit from them.
- Divmod Nevow is a templating toolkit that provides its own resource model and simple application server API. It requires Twisted, is fully integrated with the Twisted reactor, provides mechanisms for asynchronous page rendering, and an event-driven COMET/AJAX implementation called Nevow Athena. Nevow uses Twisted Web, but provides its own Request and resource model on top of it.
- Divmod Mantissa is a full-featured multi-protocol application server, with a built-in object database, authentication model based on Twisted Cred, a capability-based security model, and many more features. It integrates Nevow (and therefore Twisted Web) and a number of other Twisted-related technologies to provide a full-featured system.
So You Want To Be A Web Developer
Having reviewed this list, the obvious question is, which of these tools should I use for my application??? The short answer is: "It depends."
You can always ask about these kinds of issues on the Twisted Web mailing list, but here's a cheat-sheet that you may want to read in advance, listing some of the things that you might want to be doing with Twisted and the consensus as to the correct (or multiple possible correct) solutions for each case.
I want a web UI for my existing Twisted networking application
Assuming that your networking application has its own data persistence, or does not require persistence, and needs a web-based control panel or monitoring UI, you probably want to use Nevow. Nevow provides backwards compatibility between releases (as does Twisted Web, on which it is built), just like all Twisted projects. It also provides a templating system which makes implementing user interfaces that integrate with the event loop much easier than coding directly to the Twisted Web API. Nevow also has support for the AJAX and COMET, via Nevow Athena. Athena lets you take advantage of the event loop on multiple levels; you can push events from network triggers straight out to a web page.
I want to write a REST / WS-* interface to my Twisted application
In this case your best bet is to use Twisted Web directly. You won't need XHTML templating, so Nevow buys you little extra.
Twisted Web already has basic support for XMLRPC and SOAP, and it is fairly straightforward to set up a web service. Twisted Web also provides various facilities for being set up behind a reverse-proxy, which is the suggested mechanism to integrate your Twisted application with an existing site.
I Want a Web Framework
I want to write a Wiki
I want to write a blog
I want something to compare to Zope or TurboGears or Django or Ruby on Rails
The short version is that this is more complicated in Twisted than in other tools at the moment. Sadly, you aren't going to find a straight-up competitor with the sort of polished web presence and easy instant integration with your SQL database and existing environment that the aforementioned products offer.
Zope 3 uses Twisted now, and the next version of Zope 2 will have experimental Twisted support! You can just use that, if you are willing to delve into their innards to find the appropriate integration point for Twisted code and Zope code :).
Divmod Mantissa is a full-featured Twisted-powered application server and will likely appeal to you if you want an integrated database and other high-level framework support.
I want a container for my WSGI application
In this case it's suggested that you use Twisted Web 2. The instability of the API doesn't concern you, since the WSGI interface is well-defined and will not change. In this sense you are really just using the webserver as a tool rather than an API. Zope 3 has been using it in this capacity and (so far) it seems to be working fine. At some point, Twisted Web will provide a WSGI container and it will make sense to switch to that (a simple change).