LINUX ZONE FEATURE: The Twisted Framework Part Three, Stateful Web Servers and Templating David Mertz, Ph.D. Selector, Gnosis Software, Inc. June, 2003 In the previous installment of this series, David looked at some higher-level techniques for writing Web services, including serving dynamic pages using the .rpy extension. In this article, he moves on to look at dynamic Web serving, and how to generate dynamic Web pages using the Woven application for templating pages. INTERACTING WITH A WEB BROWSER ------------------------------------------------------------------------ In the last installment of this series, we looked at dynamic web pages served by Twisted using the '.rpy' extension. But those initial versions of a -weblog server- were only minimally dynamic. HTML tags were used to force a page to refresh periodically, and upon each refresh a bit of calculation was done to determine the relevant recent hits. But there was no user configuration aspect to the server. The first thing we will look at in this installment, therefore, is how to configure user interaction into the same basic dynamic page framework we looked at before. But even before that, let me include a quick reminder of how to launch a Twisted web server for readers who may not have seen the prior installments. Creating an "pickled application" is usually the best approach, and it can be done purely with command line options. You do not -have- to do it this way. If you like, you are free to include some extra capabilities inside the basic webserver (such as maintaining persistent data across users and sessions), but it is not necessary to write any custom code. Creating the pickled application looks something like: mktap web --path ~/twisted/www --port 8080 Launching it consists of: twistd -f web.tap That's it. Any HTML or '.rpy' files that happen to be in the '~/twisted/www' base directory (or subdirectories) will be served to clients on port 8080. Actually, you can serve whatever file type you like, but the '.rpy' files will be treated as special dynamic scripts. The dynamic page 'config_refresher.rpy' is a bit longer than any presented in the prior installment; much of that is simply the fact that it includes HTML templates in its body rather than importing them. Let us first look at the setup code #------ Dynamic script config_refresher.py (setup) -------# from twisted.web import resource, server from persist import Records from webloglib import log_fields, COLOR from urllib import unquote_plus as uqp fieldnames = """ip timestamp request status bytes referrer agent""".split() field_dict = dict(zip(fieldnames, range(len(fieldnames))) Other than a few imports that we have seen in prior installments, I map field names to their positions in the tuple returned by 'log_fields()'. Do notice also the use of a custom [persist] module that will hold the weblog in memory within the Twisted web server, so that the whole log file does not need to be read each time any client requests some records. Next the HTML templates: #--------- config_refresher.py script (templates) --------# TOP = '''