|
1
|
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.
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. |
3 comments
Add a comment
Sign in to comment
Download
Copy to clipboard

Stoppable HTTP Server. Here is a similar example:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425210
I did search, but your recipe didn't turn up (or it was somewhere way down on the list).
So all credits go to Dirk. He came up with it first.
Credits. Hi Rogier, thanks for the credits ;-) Your code is more compact than mine and I think it is a good variation for a solution over the same problem we both had with blocking servers.