Python - BaseHTTPServer - Overriding "handle_timeout" method
<p>I'm currently working with Python's <code>BaseHTTPServer</code> and I need to override the <code>handle_timeout()</code> method of the server.</p>

<p>According to Python's documentation (<a href="https://docs.python.org/2/library/basehttpserver.html" rel="nofollow">https://docs.python.org/2/library/basehttpserver.html</a>), BaseHTTPServer is a subclass of TCPServer. TCPServer implements the method <code>handle_timeout</code> (<a href="https://docs.python.org/2/library/socketserver.html#SocketServer.BaseServer.handle_timeout" rel="nofollow">https://docs.python.org/2/library/socketserver.html#SocketServer.BaseServer.handle_timeout</a>) which I want to override.</p>

<p>My code looks currently like this:</p>

<pre><code>class newServer(BaseHTTPServer.HTTPServer):

def handle_timeout(self):
    print "Timeout!"

class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):

def setup(self):
    self.timeout=2
    BaseHTTPServer.BaseHTTPRequestHandler.setup(self)  


if __name__ == '__main__':

server=newServer

httpd = server((HOST_NAME, PORT_NUMBER), RequestHandler)
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
try:
    httpd.serve_forever()
except KeyboardInterrupt:
    pass


httpd.server_close()
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
</code></pre>

<p>The timeout itself works, I receive the following message from the server:</p>

<pre><code>Request timed out: timeout('timed out',)
</code></pre>

<p>The problem is, that this is not the message I want to print.
Any help with my problem?</p>

<p>For anyone interested, after trying a few things out I finally came to a conclusion:</p>

<p>According to this file (<a href="https://svn.python.org/projects/python/trunk/Lib/BaseHTTPServer.py" rel="nofollow">https://svn.python.org/projects/python/trunk/Lib/BaseHTTPServer.py</a>), I directly copied the code from <code>BaseHTTPRequestHandler</code>'s <code>handle_one_request</code>-method and have overridden the last few lines to finally make my code work.</p>

<p>All my other attempts (overriding it in the server class, catching exceptions in the request handler or in the <code>main</code>-method) did not seem to work.</p>

<p>Hope this could help somebody out sometimes.</p>

python timeout basehttpserver