Problem exiting application in Windows Console.

Ant antroy at gmail.com
Wed Nov 8 13:10:54 CET 2006


Hi all,

I'm putting together a simple help module for my applications, using
html files stored in the application directory somewhere. Basically it
sets up a basic web server, and then uses the webbrowser module to
display it in the users browser. I have it set up to call sys.exit(0)
if the url quit.html is called, but for some reason (on Windows XP via
the cmd.exe shell) python won't let me have the console back. I get the
System Exit stack trace OK:

Exiting...
----------------------------------------
Exception happened during processing of request from ('127.0.0.1',
3615)
Traceback (most recent call last):
...
  File "C:\Documents and Settings\aroy\My Do...
    sys.exit(0)
SystemExit: 0
----------------------------------------


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.

Here's the code:

import webbrowser, os, sys
from threading import Thread
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

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):
    os.chdir(base_dir)
    server_address = ('', 8000)
    httpd = server_class(server_address, handler_class)
    server_thread = Thread(target=httpd.serve_forever)
    server_thread.start()

    webbrowser.open("http://localhost:8000/index.html")

    print "Hit CTRL-Break or CTRL-C to exit server"

def main():
    current_dir = os.path.split(sys.argv[0])[0]
    help(current_dir)

if __name__ == "__main__":
    main()




More information about the Python-list mailing list