SocketServer and Ctrl-C on Windows

Paul Moore p.f.moore at gmail.com
Mon Apr 3 05:08:43 EDT 2017


I know I've seen this before, but for the life of me I can't find any reference.

If I write a simple web server using wsgiref, something like

    from wsgiref.simple_server import make_server, demo_app

    with make_server('', 8000, demo_app) as httpd:
        print("Serving HTTP on port 8000...")

        # Respond to requests until process is killed
        httpd.serve_forever()

(from the docs), then run it on Windows, the server won't respond to Ctrl-C. I know I've seen a solution that traps ctrl-C and shuts the server down gracefully, but I can't for the life of me remember how to do it.

Can anyone point me to a way to do this? It's not crucial - I can use Ctrl-Break to stop the server - but I'd like an option for a clean shutdown if possible.

Thanks,
Paul


More information about the Python-list mailing list