SocketServer and Ctrl-C on Windows

eryk sun eryksun at gmail.com
Mon Apr 3 08:22:09 EDT 2017


On Mon, Apr 3, 2017 at 9:08 AM, Paul  Moore <p.f.moore at gmail.com> wrote:
> 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.

It works for me when run from a command prompt in Windows 10.
serve_forever() uses select() with a timeout of 0.5s, so it doesn't
block the main thread.



More information about the Python-list mailing list