tags:

views:

110

answers:

4

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.

+1  A: 

Raise exeption and handle it in main or use sys.exit

ralu
Both of these seem to be swallowed by Bottle.
Daniel Straight
+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
+2  A: 

We use this.

import os
os._exit(3)

To crash in a 'controlled' way.

S.Lott
Maybe not as controlled as it could be, but this works.
Daniel Straight
@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
Fortunately, I'm doing neither logging nor databasing (nor disk I/O), so this should be good.
Daniel Straight
+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
Any windows equiv.?
Daniel Straight