Catching Ctrl-C on Unix



examples/signals/ctrl_c.py
username = input('Username:')
print(username)

$  python ctrl_c.py


Username:^CTraceback (most recent call last):
  File "ctrl_c.py", line 3, in <module>
    username = input('Username:')
KeyboardInterrupt


examples/signals/catch_ctrl_c.py
import signal

def handler(signum, frame):
    print('Signal handler called with signal', signum)

signal.signal(signal.SIGINT, handler)

username = input('Username:')
print(username)