python 3.x - KeyboardInterrupt Exception works sometimes? -
i have simple python script:
import socket import sys try: s = socket.socket(socket.af_inet, socket.sock_raw, socket.getprotobyname("icmp")) except socket.error msg: print("could not open socket connection!") print(msg) sys.exit(1) try: s.bind(("", 0)) print("starting listener...") while true: buff = s.recvfrom(65535) print(buff[1][0]) except keyboardinterrupt: s.close() print("\nmanually quitting...") sys.exit(3) except socket.error msg: s.close() print("socket connection failed!") print(msg) sys.exit(2) except: print("something went wrong! quitting...") sys.exit(4) s.close()
when run script python 3.2.3, ctrl-c keyboard exception not caught time, means works sometimes. in fact error message different when trying ctrl-c program @ arbitrary moment. here output on console when script ran 3 times right after another:
$ sudo python3 listener.py starting listener... ^ctraceback (most recent call last): file "listener.py", line 14, in <module> buff = s.recvfrom(65535) keyboardinterrupt during handling of above exception, exception occurred: traceback (most recent call last): file "listener.py", line 17, in <module> s.close() file "/usr/lib/python3.2/socket.py", line 194, in close def close(self): keyboardinterrupt $ sudo python3 listener.py starting listener... ^ctraceback (most recent call last): file "listener.py", line 14, in <module> buff = s.recvfrom(65535) keyboardinterrupt during handling of above exception, exception occurred: traceback (most recent call last): file "listener.py", line 14, in <module> buff = s.recvfrom(65535) keyboardinterrupt $ sudo python3 listener.py starting listener... ^c manually quitting...
it worked last time. how come works sometimes!? doing wrong?
if examine stack trace notice there two exceptions being dealt with:
traceback (most recent call last): file "listener.py", line 14, in <module>
one on line 14 (in try
block); , next 1 either again on line 14 or on line 17 (which in "manually quiting" block).
it looks me have hardware problem keyboard , sends 2 <ctrl-c>
s instead of one.
Comments
Post a Comment