我想在Python脚本中捕获SIGINT信号。但是,只有当用户在发送SIGINT后输入正确的密码时,脚本才会终止。这可以用Python实现吗?一些示例代码说明了这个问题:
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)
编辑:将变量“pass”更改为“password”,如下面的注释所示。你知道吗
您可以捕获
SIGINT
并将调用分派给函数。你知道吗例如
编辑以添加我已验证有效的代码,而不是内存中的示例:)
相关问题 更多 >
编程相关推荐