Catching SIGINT with conditional exit in Python -
i want catch sigint signal in python script. however, script should terminate if user enters correct password after sending sigint. can done in python? sample code illustrate problem:
import signal def signal_handler(signal, frame): password = raw_input("enter password: ") if password != "secret": print("wrong password!") # todo don't let process end else: print("password correct. exiting now.") signal.signal(signal.sigint, signal_handler)
edit: changed variable 'pass' 'password', suggested in comments below.
you can capture sigint
, dispatch call off function.
e.g.
import signal import sys import time def confirm_password(signal, frame, secret="shhh"): password = raw_input("enter password: ") if password == secret: sys.exit(0) else: print("password incorrect. refusing exit.") while true: signal.signal(signal.sigint, confirm_password) time.sleep(1000)
edit add code i've verified work, not example memory :)
Comments
Post a Comment