|Version 6 (modified by terrycojones, 8 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, more powerful and efficient resource publishing model. Unfortunately, it is currently still a work in progress. Some aspects of its design are not finalized, but it is ready for interested early-adopting developers. If you are going to use Web 2, you really need to stay current with recent developments in Twisted; subscribe to Twisted RSS feeds of changes to trunk, etc.
- 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. Currently Nevow uses Twisted Web but it will be ported to Twisted Web 2 once that stabilizes.
- 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, 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. While not completely stable, Nevow provides some degree of backwards compatibility between releases, and will insulate you from the upcoming Twisted Web -> Twisted Web 2 transition. It will also make designing user interfaces that integrate with the event loop much easier than coding directly to either the Web or Web 2 APIs.
If you want a spiffier interface and you are willing to invest a bit more work, most of these comments about relative API stability also apply to Nevow Athena, and 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.
Unfortunately neither Nevow nor Athena is completely stable; there has not yet been a 1.0 release. But neither are they churning rapidly and requiring up-to-the-minute monitoring of development. They also have an active community that understands Twisted integration issues well.
I want to write a REST / WS-* interface to my Twisted application
In this case your best bet is to use Twisted Web directly. Porting to Twisted Web 2 for these kinds of applications will be fairly trivial (changing a few imports, method names).
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. In this case the instability of the API shouldn't concern you much, since your application should already be written to the stable (standardized, even) WSGI API. In this sense you are really just using the webserver as a tool rather than an API, and Twisted Web 2 is ready for that kind of use. Zope 3 has been using it in this capacity and (so far) it seems to be working fine.