I am looking for a simple Python webserver that is easy to kill from within code. Right now, I'm playing with Bottle, but I can't find any way at all to kill it in code. If you know how to kill Bottle (in code, no Ctrl+C) that would be super, but I'll take anything that's Python, simple, and killable.
Both of these seem to be swallowed by Bottle.
Daniel Straight
2009-10-29 12:33:59
+1
A:
Try putting
import sys
at the top and the command
sys.exit(0)
In the code that handles the "kill request".
Otto Allmendinger
2009-10-29 12:31:20
@Daniel Straight: We called it "controlled if you don't mind the consequences". We use it mostly for automated testing where incomplete log entries or trashed databases don't matter.
S.Lott
2009-10-29 16:52:29
Fortunately, I'm doing neither logging nor databasing (nor disk I/O), so this should be good.
Daniel Straight
2009-10-29 16:55:22
+2
A:
If you want to kill a process from Python, on a Unix-like platform, you can send signals equivalent to Ctrl-C at the console using Pythons os module e.g.
# Get this processes PID
pid_of_process = os.getpid()
# Send the interrupt signal to this process
os.kill(pid_of_process, signal.SIGINT)
grrussel
2009-10-29 13:33:47