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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -