BaseHTTPServer and class variables

Y

Yin

Hello.

I am using the basehttpserver to implement the HTTP protocol to serve
a fairly large lexicon that I have loaded as a dictionary in python.
Rather than writing a whole server, I would like to reuse the
BaseHTTPserver classes. I am interested in finding a way to serve the
dict without loading the whole dict into memory everytime an HTTP
request is made. The dict lives in local memory when it is loaded and
takes a long time to load.

Unfortunately with the basehttpserver.httpserver and
basehttpserver.requesthandlerclass, I am not finding it easy to define
a method to load the dict initially. Any thoughts are appreciated.

Ideally, in a do_GET for the requesthandlerclass, it'd be nice to be
able to access the dict as a class variable, but this doesn't work for
me.

Thanks.
Yin
 
E

Eddie Corns

I am using the basehttpserver to implement the HTTP protocol to serve
a fairly large lexicon that I have loaded as a dictionary in python.
Rather than writing a whole server, I would like to reuse the
BaseHTTPserver classes. I am interested in finding a way to serve the
dict without loading the whole dict into memory everytime an HTTP
request is made. The dict lives in local memory when it is loaded and
takes a long time to load.
Unfortunately with the basehttpserver.httpserver and
basehttpserver.requesthandlerclass, I am not finding it easy to define
a method to load the dict initially. Any thoughts are appreciated.
Ideally, in a do_GET for the requesthandlerclass, it'd be nice to be
able to access the dict as a class variable, but this doesn't work for
me.

import BaseHTTPServer

class Serv (BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET (self):
path = self.path.strip('/').split('/')
self.send_response (200)
self.send_header ('Content-type', 'text/html')
self.end_headers()
self.wfile.write (self.dict[path[-1]])

# load dictionary
D={'foo':1, 'bar':2}
server = BaseHTTPServer.HTTPServer (('', 8080), Serv)
server.RequestHandlerClass.dict = D
server.serve_forever()

Possibly not the best way but it should do unless someone less lazy than I
responds. (Assuming I'm understanding your request correctly).

Eddie
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top