Wednesday, May 30, 2012

How to Pass Some Arguments to BaseHTTPRequestHandler in Python

Let's say we have an argument, which is a function that we want to pass to BaseHTTRequestHandler to handle a request and return an appropriate response based the request. By looking at HTTPServer class definition, it may not be really obvious how to do it.

It's actually pretty simple and straightforward how to do it. First we need to subclass HTTPServer and override the constructor that takes in our own new arguments. From the RequestHandler class, there'll be a property named server that gives us access to the HTTPServer instance. And since we've subclassed HTTPServer class, we can define as many properties as we want that those properties will be accessible in the server property of the HTTPRequestHandler. The example is shown below.

#!/usr/bin/env python

import logging
from BaseHTTPServer import HTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler

class HttpServerHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        content_length = int(self.headers.getheader("Content-Length"))
        request ="Request: %s" % request)
        # BaseHTTPRequestHandler has a property called server and because
        # we create MyHTTPServer, it has a handler property
        response = self.server.handler(request)"Response: %s" % response)
class MyHTTPServer(HTTPServer):
    """this class is necessary to allow passing custom request handler into
       the RequestHandlerClass"""
    def __init__(self, server_address, RequestHandlerClass, handler):
        HTTPServer.__init__(self, server_address, RequestHandlerClass)
        self.handler = handler
class HttpServer:
    def __init__(self, name, host, port, handler): = name = host
        self.port = port
        self.handler = handler
        self.server = None
    def start(self):'Starting %s at %s:%d' % (,, self.port))
        # we need use MyHttpServer here
        self.server = MyHTTPServer((, self.port), HttpServerHandler,
    def stop(self):
        if self.server:
  'Stopping %s at %s:%d' % (,,

def server_handler(request):
    if request == "foo":
        return "bar"
    elif request == "bar":
        return "foo"
        return "foobar"

if __name__ == "__main__":
    logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', 
    server = HttpServer("test server", "localhost", 9999, server_handler)

No comments:

Post a Comment