Wednesday, March 17, 2010

Simple Zope HTTP Server

I've seen mention of every conceivable Python web framework as of late. Mostly performance comparisons. One I don't see that often is Zope. It is a mature framework that does everything under the sun. I think it isn't mentioned that often by developers because of the lack of documentation.

The good news is that Zope code is easy to read and understand. I decided to experiment and create a really simple HTTP server with Zope HTTPServer. I find the task-oriented view of each HTTP request both interesting and useful. Here is what I came up with:

import asyncore
from zope.server.http.httpserver import HTTPServer
from zope.server.http.httptask import HTTPTask
from zope.server.taskthreads import ThreadedTaskDispatcher

class MyServer(HTTPServer):

def executeRequest(self, task):

task.start()

self.log_info("Starting request task.")

task.response_headers['Content-Type'] = 'text/plain'
task.response_headers['Content-Length'] = 5

task.write(task.request_data.command)
task.write(" ")
task.write(task.request_data.uri)

task.finish()

self.log_info("Rquest task finished.")

if __name__ == "__main__":

dispatcher = ThreadedTaskDispatcher()
dispatcher.setThreadCount(10)

MyServer('', 8084, task_dispatcher = dispatcher)

try:
while True:
asyncore.poll(5)
except KeyboardInterrupt:
dispatcher.shutdown()

No comments :

Post a Comment