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

Ant antroy at gmail.com
Wed Nov 8 16:54:05 CET 2006

Ant wrote:
> However, at this point instead of getting back to a command prompt, I
> get an unresponsive console. Hitting CTRL-Break gets me the command
> prompt back, but I would have expected to get back the command prompt
> as soon as the sys.exit(0) had completed.
> class HelpHTTPRequestHandler(SimpleHTTPRequestHandler):
>     def do_GET(self):
>         print "PATH: ", self.path
>         if self.path.endswith("quit.html"):
>             print "Exiting..."
>             sys.exit(0)
>         else:
>             return SimpleHTTPRequestHandler.do_GET(self)
> def help(base_dir, server_class=HTTPServer,
> handler_class=HelpHTTPRequestHandler):

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?

The default handle_error method in the TCPServer uses the traceback
module to print the stacktrace, but I can't find anything in that
module to get the actual exception object (or string) - is there an
alternative trick?

Incidentally, this seems to me to be a pretty drastic try: except:
block, catching *everything* and then (essentially) discarding the
exception. Wouldn't it be better to either catch only the exceptions
that are expected (presumably IO errors, Socket exceptions, HTTP error
code exceptions etc), and let others pass through, or alternatively
pass the exception through to the handle_error() method since this is
where it should be dealt with?


More information about the Python-list mailing list