10

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?

1
  • 1
    Catch the signal, handle appropriately. The concept is language agnostic, you'll have to find a way to handle signals in python specifically. Commented Aug 13, 2015 at 8:51

3 Answers 3

21

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]

https://docs.python.org/2/library/signal.html

Sign up to request clarification or add additional context in comments.

5 Comments

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.
You means catch exception everywhere? I mean shutdown the signal in one place.
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.
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
I got an EOFError in Python 3 if the user pressed Ctrl+C while a user input prompt was open.
6

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

@SDilmac Why isn't this a good idea? It's perfectly documented that the default signal handler for Ctrl+C raises a KeyboardInterrupt exception...
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 processes
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?
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.
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.
0

Use this for a repetitive task. But in GNU/Linux, it shows "^C" when Ctrl-C is hit. (Tested on Windows Store app Ubuntu, should work on many platforms)

def repeat():
    try:
        import time
        x=0
        while x==0:
            print("This is text")
            time.sleep(0.1)
    except KeyboardInterrupt:
        repeat()
repeat()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.