fix signal issue in Windows
also refactor code and incorporate some "pythonic" patterns
This commit is contained in:
parent
8322a83e52
commit
20d007d672
25
dnseval.py
25
dnseval.py
@ -60,12 +60,9 @@ def signal_handler(sig, frame):
|
|||||||
should_stop = True # pressed once, exit gracefully
|
should_stop = True # pressed once, exit gracefully
|
||||||
|
|
||||||
|
|
||||||
def widest_len(names):
|
def maxlen(names):
|
||||||
width = 8
|
sn = sorted(names, key=len)
|
||||||
for s in names:
|
return len(sn[-1])
|
||||||
if len(s) > width:
|
|
||||||
width = len(s)
|
|
||||||
return width
|
|
||||||
|
|
||||||
|
|
||||||
def dnsping(host, server, dnsrecord, timeout, count):
|
def dnsping(host, server, dnsrecord, timeout, count):
|
||||||
@ -114,8 +111,11 @@ def dnsping(host, server, dnsrecord, timeout, count):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
signal.signal(signal.SIGTSTP, signal.SIG_IGN) # ignore CTRL+Z
|
try:
|
||||||
signal.signal(signal.SIGINT, signal_handler) # ignore CTRL+C
|
signal.signal(signal.SIGTSTP, signal.SIG_IGN) # ignore CTRL+Z
|
||||||
|
signal.signal(signal.SIGINT, signal_handler) # ignore CTRL+C
|
||||||
|
except AttributeError: # Some systems may not support all signals
|
||||||
|
pass
|
||||||
|
|
||||||
if len(sys.argv) == 1:
|
if len(sys.argv) == 1:
|
||||||
usage()
|
usage()
|
||||||
@ -163,15 +163,16 @@ def main():
|
|||||||
f = resolvers
|
f = resolvers
|
||||||
if len(f) == 0:
|
if len(f) == 0:
|
||||||
print("No nameserver specified")
|
print("No nameserver specified")
|
||||||
width = widest_len(f)
|
|
||||||
|
f = [ name.strip() for name in f]
|
||||||
|
width = maxlen(f)
|
||||||
blanks = (width - 5) * ' '
|
blanks = (width - 5) * ' '
|
||||||
print('server ', blanks, ' avg(ms) min(ms) max(ms) stddev(ms) lost(%)')
|
print('server ', blanks, ' avg(ms) min(ms) max(ms) stddev(ms) lost(%)')
|
||||||
print((60 + width) * '-')
|
print((60 + width) * '-')
|
||||||
for server in f:
|
for server in f:
|
||||||
s = server.strip()
|
if not server:
|
||||||
if not s:
|
|
||||||
continue
|
continue
|
||||||
(server, r_avg, r_min, r_max, r_stddev, r_lost_percent) = dnsping(hostname, s, dnsrecord, waittime, count)
|
(server, r_avg, r_min, r_max, r_stddev, r_lost_percent) = dnsping(hostname, server, dnsrecord, waittime, count)
|
||||||
|
|
||||||
server = server.ljust(width + 1)
|
server = server.ljust(width + 1)
|
||||||
print("%s %-8.3f %-8.3f %-8.3f %-8.3f %%%d" % (
|
print("%s %-8.3f %-8.3f %-8.3f %-8.3f %%%d" % (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user