Search code examples
pythonbasehttpserver

Run Script on Get and Return response immediately


Apologies but I am super unfamiliar with Python httpserver. I am also aware of the vulnerabilities or limitations of it, but it is ideal for my needs.

I have about 10 lines of code that are running on every get request to the server (based on things in the request header). These can take up to 8 hours to completely run, but I would like to return a response for the request immediately. What is the best method of doing this?

from http.server import BaseHTTPRequestHandler, HTTPServer
import logging


class S(BaseHTTPRequestHandler):
    def _set_response(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        THIS IS WHERE MY CODE IS RUNNING
        logging.info("GET request,nPath: %snHeaders:n%sn", str(self.path), str(self.headers))
        self._set_response()
        self.wfile.write("HEY A GET request for {}".format(self.path).encode('utf-8'))


def run(server_class=HTTPServer, handler_class=S, port=8080):
    logging.basicConfig(level=logging.INFO)
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    logging.info('Starting httpd...n')
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    logging.info('Stopping httpd...n')


if __name__ == '__main__':
    from sys import argv

    if len(argv) == 2:
        run(port=int(argv[1]))
    else:
        run()

Thanks


Solution

  • This is exactly what multi-threading is used for, I suggest you read this tutorial to learn more about it.

    here is an example of how create a new thread from inside the do_GET function:

    from http.server import BaseHTTPRequestHandler, HTTPServer
    import logging
    import thread
    
    
    class S(BaseHTTPRequestHandler):
        def _set_response(self):
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
    
    def do_process(self):
    # Your code here
    
    def do_GET(self):
        thread.start_new_thread(self.do_process)
        logging.info("GET request,nPath: %snHeaders:n%sn", str(self.path), str(self.headers))
        self._set_response()
        self.wfile.write("HEY A GET request for {}".format(self.path).encode('utf-8'))
    
    
    def run(server_class=HTTPServer, handler_class=S, port=8080):
        logging.basicConfig(level=logging.INFO)
        server_address = ('', port)
        httpd = server_class(server_address, handler_class)
        logging.info('Starting httpd...n')
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            pass
        httpd.server_close()
        logging.info('Stopping httpd...n')
    
    
    if __name__ == '__main__':
        from sys import argv
    
        if len(argv) == 2:
            run(port=int(argv[1]))
        else:
            run()