BaseHTTPServer blocks while waiting for a connection. This means that a script will not respond to anything until it receives a network connection, which may never come. By adding a timeout to the listening socket, the script will regain control every so often.
Python, 25 lines
I use a small http server like this to quickly transfer files between computers without all the overhead of more advanced filesharing programs.
After starting a BaseHTTPServer with serve_forever(), I cannot use a KeyboardInterrupt to quit the server, unless I request another page. With a timeout I may only have to wait up to 10 seconds.
The timeout on the new socket must be reset, because the request handlers use socket operations that do not work on a non-blocking socket.