title: Python server will not terminate on Ctrl+C after a request has come in
body: <p>I'm dabbling with Python and HTTP. Have created this simple server script I run with <code>python cli.py serve</code>. The server starts and works, but the keyboard interrupt Ctrl+C only gets trough on the next page refresh, not when I actually press Ctrl+C in the terminal.. Any fix for this? Note: works immediately when no request have come in yet.</p>
  
  <pre><code>from http.server import HTTPServer, BaseHTTPRequestHandler
  import sys, signal
  
  commands = ["serve"]
  
  class Server(BaseHTTPRequestHandler):
      def do_GET(self):
          if self.path == "/":
              self.path = "/index.html"
          self.send_response(200)
          self.end_headers()
          self.wfile.write(bytes("Tere, maailm!!",'utf-8'))
  
  
  if len(sys.argv) &gt; 1 and sys.argv[1] in commands:
      index = commands.index(sys.argv[1])
      if index == 0: # serve command
          print("Starting web server. Press Ctrl+C to exit!")
          httpd = HTTPServer(("localhost", 8080), Server)
          try:
              httpd.serve_forever()
          except KeyboardInterrupt:
              print("Shutting down...")
              httpd.socket.close()
              sys.exit(0)
  else:
      print("Usage: python tab.py [command], where [command] is any of", commands)
  </code></pre>