Search code examples
python-3.xthree.jshttpserver

Python HTTPServer [WinError 10053] When there are too many requests?


Running a super simple webserver that, for the most part, just needs to strip out the query info from requests from a 3rd party script and return some files:

def run(server_class=HTTPServer, handler_class=MyHandler, port=xxx):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print ('Starting httpd...')
httpd.serve_forever()

One object that the server must return is an HTML page that then loads some javascript, which then loads three.js, which then proceeds to load a ton of objects from this same server.

Which works, but after reloading 1-5 times, it would usually result in WinError[10053] and the server locking up! After that, connections would be rejected or timeout. Not sure if this is due to too many requests, or something to do with Three.js's load function's connections.

This took hours and I couldn't find a specific solution, so I'll post an answer down below. Feel free to chime in other answers


Solution

  • By multi-threading the server/handler, I've been able to handle at least one user. It may still be a throughput problem, but this has been enough for now:

    class ThreadingHTTPServer(socketserver.ThreadingMixIn, HTTPServer):
        pass
    
    def run(server_class=ThreadingHTTPServer, handler_class=MyHandler, port=xxx):
        server_address = ('', port)
        httpd = server_class(server_address, handler_class)
        print ('Starting httpd...')
        httpd.serve_forever()
    

    Some other things I tried/failed:

    • Editing self.path and calling SimpleHTTPRequestHandler.do_GET(self) //Couldn't get path changing to affect the simple handler
    • allow_reuse_address = True/False // No effect
    • manually setting close_connection = True //No effect
    • some header play //No effect

    Hope this saves someone!