Exception Handling in TCPServer (was; Problem exiting application in Windows Console.)

Ant antroy at gmail.com
Wed Nov 8 12:30:18 EST 2006


Ant wrote:
...
> OK, I've narrowed the problem back to the way HTTPServer (actually its
> TCPServer parent) handles exceptions thrown by the process_request
> method by catching them all, and then calling a handle_error method.
> There doesn't seem to be a way of getting at the exception thrown
> however - does anyone know how I can get this information?

Hmm. Lonely topic ;-)

I've found a way to solve the problem, by creating a subclass of
HTTPServer which overrides the handle_error method:

class HelpServer(HTTPServer):
    def handle_error(self, request, client_address):
        exception_line = inspect.trace()[-1][-2][0]
        if "sys.exit" in exception_line:
            print "Trying to exit again!"
            sys.exit(0)
        else:
            HTTPServer.handle_error(self, request, client_address)

This seems a really nasty hack though - any ideas for a cleaner way to
do it?




More information about the Python-list mailing list