proper ctrl+c handling

This commit is contained in:
Babak Farrokhi 2016-02-06 09:36:01 +03:30
parent fdc300eb8d
commit dd7e6a745f
2 changed files with 14 additions and 7 deletions

View File

@ -1,10 +1,9 @@
# dnstools
DNS Diagnostics and Performance Measurement Tools
# prerequirements
# prerequisites
This script requires python3 as well as latest [dnspython](http://www.dnspython.org/)
# todo
- input sanitization
- proper signal handling

View File

@ -34,6 +34,7 @@ import dns.rdatatype
import dns.resolver
__VERSION__ = 1.0
should_stop = False
def usage():
@ -50,10 +51,16 @@ def usage():
exit()
def signal_handler(signal, frame):
global should_stop
if should_stop: ## pressed twice, so exit immediately
exit(0)
should_stop = True ## pressed once, exit gracefully
def main():
# if not __debug__:
# signal.signal(signal.SIGTSTP, signal.SIG_IGN) # ignore CTRL+Z
# signal.signal(signal.SIGINT, signal.SIG_IGN) # ignore CTRL+C
signal.signal(signal.SIGTSTP, signal.SIG_IGN) # ignore CTRL+Z
signal.signal(signal.SIGINT, signal_handler) # ignore CTRL+C
if len(sys.argv) == 1:
usage()
@ -108,6 +115,8 @@ def main():
print("DNSPING %s: hostname=%s rdatatype=%s" % (dnsserver, hostname, dnsrecord))
for i in range(count):
if should_stop:
break
try:
stime = time.time()
answers = resolver.query(hostname, dnsrecord)
@ -156,4 +165,3 @@ def main():
if __name__ == '__main__':
main()