While Twisted.Web2 can be deployed in a variety of flexible and complicated ways, occasionally a simpler approach is desired. For this Web2 makes use of TAPs created by the mktap commandline utility. This document outlines a few of the approaches for creating, configuring, and deploying a Twisted.Web2 with mktap.
Since the Web2 mktap plugin is a work in progress it is suggested that you refer to the output of the following command for further information
% mktap web2 --help
Simple Servers
Static Files
Perhaps the simplest possible Twisted.Web2 configuration is to serve a bunch of static files from a directory.
% mktap web2 --path /some/path
In case you've forgotten mktap 101 this will create a file in the current directory called web2.tap, you can then launch this server configuration with the following command.
% twistd -nf web2 2006/03/02 00:29 PST [-] Log opened. 2006/03/02 00:29 PST [-] twistd SVN-Trunk (/usr/bin/python 2.4.2) starting up 2006/03/02 00:29 PST [-] reactor class: twisted.internet.selectreactor.SelectReactor 2006/03/02 00:29 PST [-] Loading web2.tap... 2006/03/02 00:29 PST [-] Loaded. 2006/03/02 00:29 PST [-] twisted.web2.channel.http.HTTPFactory starting on 8080 2006/03/02 00:29 PST [-] Starting factory <twisted.web2.channel.http.HTTPFactory instance at 0x7787ee4c>
You now have a HTTP server serving static files on port 8080, and if you open it in a web browser you'll see something like this in your terminal.
2006/03/02 00:29 PST [HTTPChannel,0,127.0.0.1] 127.0.0.1 - - [02/Mar/2006:00:29:14 -0700] "GET / HTTP/1.1" 200 2577 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.1) Gecko/20060224 Ubuntu/dapper Firefox/1.5.0.1"
By default the TAP plugin logs to the standard twistd logfile.
However if you specify the --logfile
option twistd will log to the specified file in the Common Access
Logging format.
% mktap web2 --path /some/path --logfile ./access.log
Dynamic Resources
Twisted.Web2's tap plugin is also perfectly capable of serving up a dynamic Resource or two. In the name of completeness here is a simple resource.
from twisted.web2 import http, resource class HelloWorld(resource.Resource): def render(self, req): return http.Response(200, stream="Hello, World!")
To use it from mktap you simply have to make sure it's in your PYTHONPATH and tell mktap to use it as its root resource.
% mktap web2 --class=hello.HelloWorld
It's important to keep in mind that this class will be created with no arguments passed to the constructor.
Virtual Hosts
Now for something a little different, and a little more advanced. The TAP plugin supports several ways of configuring a server that uses Named Virtual Hosting.
Just a bunch of directories
The first method of serving virtual hosts involves a bunch of directories that correspond to the root directory of a virtual host.
For example:
% ls servers test.example.com foo.example.com mail.example.com % mktap web2 --vhost-path servers/
Each of the directories under servers
is served out as
a static.File
when
you visit the appropriate url, for example
http://test.example.com:8080/ will give you the contents of
servers/test.example.com
(assuming
test.example.com points to the actual place where the server is
running.)
Adding a Single Virtual Host
You can also add a single virtual host at a time, either in
a seperate directory structure with
--vhost-static
or as a dynamic resource
with --vhost-class
. You can use as
many of these arguments as you wish, even combining them with
--vhost-path
.
For example the following command will give us a web2.tap that serves two virtual hosts, images.example.com and example.com which will serve our dynamic application ( Hello World.)
% mktap web2 --vhost-static=images.example.com=images/ --vhost-class=example.com=hello.HelloWorld
Conclusion
Web2's TAP plugin is a great way to get start a server and start playing around. However there are many other ways to deploy web2, and the TAP plugin is meant to be a stepping stone to more advanced techniques such as those mentioned in the deployment howto.