passing context into BaseHTTPRequestHandler

S

Steve Howell

I have a use case where I'm running BaseHTTPServer.HTTPServer, and I
want to configure the request handler with some context. I've gotten
the code to work, but it feels overly heavy. I am wondering if
anybody could suggest an easier idiom for this.

This is a brief sketch of the code:

class MyHandler(BaseHTTPRequestHandler):
def __init__(self, context, *args):
self.context = context
BaseHTTPRequestHandler.__init__(self, *args)

def do_GET(self):
// self.context will be available here

context = { .... }
def handler(*args):
MyHandler(context, *args)

server = HTTPServer(('', port), handler)
server.serve_forever()

Basically, it takes five lines of code just to pass context into the
handler:

def handler(*args):
MyHandler(context, *args)

def __init__(self, context, *args):
self.context = context
BaseHTTPRequestHandler.__init__(self, *args)

Typically the second argument to HTTPServer is a subclass of
BaseHTTPRequestHandler, but I pass in "handler" instead, which
instantiates a new instance of MyHandler with the context from the
enclosing scope. At first I tried to say "MyHandler(*args).context =
context", but that assignment was too late, because
BaseHTTPRequestHandler.__init__ does a lot of stuff during the
construction phase. That's what forced me to write my own custom
__init__ as well.

I hope there's enough info here to understand what I'm trying to
achieve, but if it helps to see my code in more context, you can see
it here:

https://gist.github.com/2173618

Thanks.
 
S

Steve Howell

You could store the context in the server object and access it in the
handler methods via self.server.context. It basically works like this:

class MyHTTPServer(HTTPServer):

    def __init__(self, *args, **kw):
        HTTPServer.__init__(self, *args, **kw)
        self.context = { .... }

class MyHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        context = self.server.context
        ...

server = MyHTTPServer(('', port), MyHandler)
server.serve_forever()

Thanks for the suggestion. I made the change, and I think the code's
a bit easier to read now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top