I'm trying to run WifiPhisher for educational purposes. At one step it says Press Ctrl-C to input a number. Now when I press Ctrl-c the script exits as I described it in this issue on github. Whereas ideally, the script shouldn't exit rather it should continue the logic after Ctrl-C is pressed. I'm not familiar with Python, can anyone help me get past this?
3 Answers
You can set a signal handler to CTRL-C signal to shutdown the default signal handler which raises a KeyboardInterrupt exception.
import signal, os
def handler(signum, frame):
print 'Signal handler called with signal', signum
# Set the signal handler
signal.signal(signal.SIGINT, handler)
Ctrl-C (in older Unixes, DEL) sends an INT signal (SIGINT); by default, this causes the process to terminate
SIGINT The SIGINT signal is sent to a process by its controlling terminal when a user wishes to interrupt the process. This is typically initiated by pressing Control-C, but on some systems, the "delete" character or "break" key can be used.[6]
5 Comments
skyking
This has a disadvantage: by installing a signal handler you will handle the signal out of context. To be able to see that the signal has been triggered at the point where the program is waiting you will (almost) have to resort to using global variables to communicate that information. For the described situation catching the signal is more suitable.
luoluo
You means catch exception everywhere? I mean shutdown the signal in one place.
skyking
No, the question says "At one step it says Press Ctrl-C to input a number." so you only have to catch the exception there. In any case you would have to include a mechanism to wait for Ctrl-C to be pressed anyway at that step.
user3677331
Can you please reform your answer to put it in continuation of the script in the question? I'm not able to make sense of the flow of this code so even though I know I have to catch the signal,I don't know how to do that in this script
Stevoisiak
I got an EOFError in Python 3 if the user pressed Ctrl+C while a user input prompt was open.
You need to catch a KeyboardInterrupt and handle that.
Really basic example:
try:
while True:
print "Hello world"
except KeyboardInterrupt:
print "Goodbye"
exit(0)
5 Comments
Bakuriu
@SDilmac Why isn't this a good idea? It's perfectly documented that the default signal handler for Ctrl+C raises a
KeyboardInterrupt exception...mproffitt
I'm thinking that the problem isn't with the use of
KeyboardInterrupt but with my example use of while True - this bit isn't so much a good idea but served only for demonstration processesuser3677331
I can't seem to make sense of the flow of this code honestly, can you point out the line in this script where praying ctrl-c continues the program to ask the input number?
jlh
The question was about "avoid quitting when Ctrl-C is pressed" and your answer does precisely the opposite: it quits when Ctrl-C is pressed.
vy32
Sometimes you want to stop what you are doing and have clean-up code. Other times you just want to set a flag and keep going. You can't keep going if you catch the exception.