Free project hosting by SourceForge

Author: Steve Purcell, Pythangelist, Available for Python & Ruby on Rails consulting/training
Latest release: 0.2, 26th March 2001 (download)

PyServ - a servlet-like engine for Python

PyServ lets the Python programmer write servlet-like HTTP request handling units whose sessions are transparently managed by a containing servlet 'engine'.


The development of a Python servlet engine has been on my mind since mid-2000. Last year I hacked up some code, then threw most of it away. In January this year I hacked some more, in a post-Christmas coding frenzy, and decided to stick the code on SourceForge and see where things go.

The prime intention of this project is that Python programmers should be able to write something like a servlet, thereby being afforded a middle way between the extremes of CGI and Zope.

There should be automatic management of sessions, a simple and sane set of request/response objects, and a variety of deployment options.

What works

The current code situation is nothing earth-shattering, but it's already fun. There is a CGI wrapper servlet engine in less than 180 lines of code such that one deploys a set of servlets by writing a little CGI script as follows:

#!/usr/bin/env python
# File 'testapp.cgi'

# Path of directory in which persistent sessions will be stored
SESSION_DIR = 'sessions'

# Module prefix of servlets
SERVLET_PREFIX = 'thisapp'

import sys
sys.path.insert(0, '../../lib') # Make pyserv module importable
import pyserv

if __name__ == '__main__':
    pyserv.cgimain(SESSION_DIR, SERVLET_PREFIX)

That specifies a self-contained servlet 'application' that will map requests to servlets in module 'thisapp'. Then we can write a module called 'thisapp' containing something like the following:

class TestServlet:
    def run(self, request, response):
        # Content type defaults to 'text/plain'
        visits = self.session.get('visits', 0)  # sessions look like dicts
        write = response.write
        write("%d previous visits" % visits)
        self.session['visits'] = visits + 1

        write("The request is: ")

        write("The headers are: ")

        write("The environment vars are: ")

If we plop the 'testapp.cgi' CGI script into 'cgi-bin' on our server, we can run our TestServlet by visiting a specific URL. I've set it up here on SourceForge for you to try out:

Try reloading the page a couple of times to see the visit counter increase. Also try adding some query parameters by appending '?blah=foo' to the URL.

Sessions are automatically created by the setting of a cookie, and are saved and loaded automatically using the standard library module 'pickle'. They are protected against concurrent access by file locking with the flock function in the fcntl; this mechanism will need to be altered for non-UNIX platforms.

On my laptop, with Apache, this set-up can handle about 10 requests per second, which is already enough to serve more than 800,000 requests per day. I tried adding a hundred or so postgres DB queries to my test servlet and still got about 5 requests per second.

Sessions have a configurable idle timeout, which defaults to one hour.

What doesn't work

Plenty of things need more work. In particular, the current handling of response headers and cookies needs work. That work will get done pretty soon, and contributions are welcome.

Old sessions are not tidied up automatically; this is easily done with a cron job.

There's no fancy logging support yet.

Future plans

In the pretty near future (really!) there will be a long-running standalone program in which to deploy servlets, along the lines of Apache JServ, which runs behind an Apache instance. Later there will be a completely standalone web server application similar to Apache Tomcat.

But hey, have a play with what's already there; maybe you don't need all that fancy stuff anyway.

[Update, 3 Sep 2001: there has been no active development in this direction for a while, due to lack of time]

Can I download it / comment on it?

Of course you can! A few caveats first:

If you're still feeling brave, you can choose from any of the following options:-

Please don't submit bugs yet, but feel free to send comments to me or to the mailing list (to which I am probably the only subscriber anyway).

Resources and links

Contacting me

Feel free to send me e-mail using this form or directly to: stephen_purcell at yahoo dot com.

I am intermittently available for Python and Ruby on Rails consulting and training engagements in various application areas and on various platforms; details available upon request. (IT recruiters will save time by not bothering to contact me.)

All release packages made using CVSUtils
Last update: 26th March 2001
Hits since 11th February 2001: