pythonhttphttphandler

How do I create a python server that can initialize variables when starting up and use those variables in request handlers?


I am trying to create a python server that needs to use some local data when handling requests. Here is my code:

httpd = HTTPServer((HOST_NAME,PORT),myHandler)
print(time.asctime(), "Start Server - %s:%s\n"%(HOST_NAME,PORT))
try:
    httpd.serve_forever()
except KeyboardInterrupt:
    pass
httpd.server_close()
print(time.asctime(),'Stop Server - %s:%s' %(HOST_NAME,PORT))

I have tried loading the local data in myHandler class, but it seems that everytime a new request is made the server reinitializes a new instance of myHandler, hence reloading the data. (which slows down the server significantly).

The myHandler class is currently written in a separate python script, so I can't create global variables on the main function.

Would it work if I merge the main script and the myHandler script so I can use global variables? Is there a better way? Thank you for your solutions.


Solution

  • I have tried loading the local data in myHandler class, but it seems that everytime a new request is made the server reinitializes a new instance of myHandler...

    That's correct. A handler is associated with a request. If you have data that should persist for the lifetime of your server, you need to make it part of the server class. E.g., we can subclass HTTPServer like this:

    import time
    from http.server import HTTPServer, BaseHTTPRequestHandler
    
    HOST_NAME = 'localhost'
    PORT = 8080
    
    class MyServer(HTTPServer):
        def __init__(self, address, handler):
            self.myData = 0
            super().__init__(address, handler)
    
    
    class myHandler(BaseHTTPRequestHandler):
        def do_GET(self, *args):
            self.send_response(200)
            self.end_headers()
            self.wfile.write(f"This is a test: {self.server.myData}\n".encode())
            self.server.myData += 1
    
    httpd = MyServer((HOST_NAME,PORT),myHandler)
    print(time.asctime(), "Start Server - %s:%s\n"%(HOST_NAME,PORT))
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    print(time.asctime(),'Stop Server - %s:%s' %(HOST_NAME,PORT))
    

    Here, we have a piece of data (self.myData) associated with the server. We can reference this in a handler by using the self.server attribute to access the server instance.