我正在尝试运行WifiPhisher用于教育目的.一步说它按Ctrl-C输入一个数字.现在,当我按下Ctrl-c时,脚本将退出,就像我在github上的这个问题中描述的那样.理想情况下,脚本不应该退出,而应该在按下Ctrl-C后继续逻辑.我不熟悉Python,任何人都可以帮我解决这个问题吗?
您可以设置signal handler到CTRL-C信号来关闭默认signal handler这引起了一个KeyboardInterrupt例外.
import signal, os
def handler(signum, frame):
print 'Signal handler called with signal', signum
# Set the signal handler
signal.signal(signal.SIGINT, handler)
Run Code Online (Sandbox Code Playgroud)
Ctrl-C(在较旧的Unix中,DEL)发送INT信号(SIGINT); 默认情况下,这会导致进程终止
SIGINT当用户希望中断该过程时,SIGINT信号由其控制终端发送到进程.这通常是通过按Control-C启动的,但在某些系统上,可以使用"删除"字符或"中断"键.[6]
https://docs.python.org/2/library/signal.html
您需要捕获KeyboardInterrupt并处理它.
真的基本的例子:
try:
while True:
print "Hello world"
except KeyboardInterrupt:
print "Goodbye"
exit(0)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4012 次 |
| 最近记录: |