Python – BaseHTTPServer – override some default method

When you start to write your own http python server, the easiest way is to start with with BaseHTTPServer class. there are many behavior that you want to customize such as the timeout , or error handling. This is the link to original BaseHTTPServer, it’s for your reference to modify it https://svn.python.org/projects/python/trunk/Lib/BaseHTTPServer.py

This is my timeout and finish override function

def setup(self):
        BaseHTTPRequestHandler.setup(self)
        self.rfile._sock.settimeout(2)

def finish(self,*args,**kw):
        try:
            if not self.wfile.closed:
                self.wfile.flush()
                self.wfile.close()
        except socket.error:
            do_debug("remote client disconnected")
            self.job_finish()
            pass
        self.rfile.close()

Leave a Reply

Your email address will not be published. Required fields are marked *