BaseHTTPServer and class variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yin

    BaseHTTPServer and class variables

    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. requesthandlerc lass, 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 requesthandlerc lass, it'd be nice to be
    able to access the dict as a class variable, but this doesn't work for
    me.

    Thanks.
    Yin
  • Eddie Corns

    #2
    Re: BaseHTTPServer and class variables

    yin_12180@yahoo .com (Yin) writes:
    [color=blue]
    >Hello.[/color]
    [color=blue]
    >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
    >BaseHTTPserv er 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.[/color]
    [color=blue]
    >Unfortunatel y with the basehttpserver. httpserver and
    >basehttpserver .requesthandler class, I am not finding it easy to define
    >a method to load the dict initially. Any thoughts are appreciated.[/color]
    [color=blue]
    >Ideally, in a do_GET for the requesthandlerc lass, it'd be nice to be
    >able to access the dict as a class variable, but this doesn't work for
    >me.[/color]

    import BaseHTTPServer

    class Serv (BaseHTTPServer .BaseHTTPReques tHandler):
    def do_GET (self):
    path = self.path.strip ('/').split('/')
    self.send_respo nse (200)
    self.send_heade r ('Content-type', 'text/html')
    self.end_header s()
    self.wfile.writ e (self.dict[path[-1]])

    # load dictionary
    D={'foo':1, 'bar':2}
    server = BaseHTTPServer. HTTPServer (('', 8080), Serv)
    server.RequestH andlerClass.dic t = D
    server.serve_fo rever()

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

    Eddie

    Comment

    Working...