Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
Fixed that -d/—duration cannot be negative
  • Loading branch information
youfou committed Jan 20, 2017
1 parent 59e9ddf commit fe2ac20
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion mping/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from .mping import mping, results_string

__title__ = 'mping'
__version__ = '0.1.1'
__version__ = '0.1.2'
__author__ = 'Youfou'
__license__ = 'Apache 2.0'
__copyright__ = 'Copyright 2017 Youfou'
59 changes: 39 additions & 20 deletions mping/mping.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ def __init__(self, host, timeout, interval):
:param interval: the max time to sleep between each ping
"""

if re.match(r'[\d.]+$', host):
self.host = host

if re.match(r'(?:\d{1,3}\.){3}(?:\d{1,3})$', host):
self.ip = host
else:
print('Resolving host: {}'.format(host))
Expand All @@ -136,20 +138,24 @@ def __init__(self, host, timeout, interval):
self.interval = interval

self.pr = PingResults()
self.terminated = False
self.finished = False

super(PingTask, self).__init__()

def run(self):
while not self.terminated:
rtt = ping(self.ip, timeout=self.timeout)
while not self.finished:
try:
rtt = ping(self.ip, timeout=self.timeout)
except OSError:
print('Unable to ping: {}'.format(self.host))
break
self.pr.append(rtt)
escaped = rtt or self.timeout
if escaped < self.interval:
time.sleep(self.interval - escaped)

def stop(self):
self.terminated = True
def finish(self):
self.finished = True


def mping(hosts, duration=DEFAULT_DURATION, timeout=1.0, interval=0.0, quiet=False, sort=True):
Expand All @@ -164,7 +170,10 @@ def mping(hosts, duration=DEFAULT_DURATION, timeout=1.0, interval=0.0, quiet=Fal
:return: A list of PingResults
"""

def ret(_tasks, _sort=True):
def results(_tasks, _sort=True):
"""
Return the current status of a list of PingTask
"""
r = list(zip(heads, [t.pr for t in _tasks]))
if _sort:
r.sort(key=lambda x: x[1].valid_count, reverse=True)
Expand All @@ -186,7 +195,7 @@ def ret(_tasks, _sort=True):
else:
doing_msg = 'Pinging {} hosts'.format(len(hosts))
if duration > 0:
doing_msg += ' within {} seconds'.format(duration)
doing_msg += ' within {} seconds'.format(int(duration))
doing_msg += '...'

if quiet:
Expand All @@ -195,26 +204,35 @@ def ret(_tasks, _sort=True):
for task in tasks:
task.start()

start = clock()
remain = duration

try:
while remain > 0 or duration <= 0:
time.sleep(min(remain, 1))
start = clock()
while True:

if duration > 0:
remain = duration + start - clock()
if remain > 0:
time.sleep(min(remain, 1))
else:
break
else:
time.sleep(1)

if not quiet:
print('\n{}\n{}'.format(
results_string(ret(tasks, True)[:10]),
results_string(results(tasks, True)[:10]),
doing_msg))
remain = duration + start - clock()

except KeyboardInterrupt:
print()
finally:
for task in tasks:
task.stop()
for task in tasks:
task.join()
task.finish()

return ret(tasks, sort)
# Maybe not necessary?
# for task in tasks:
# task.join()

return results(tasks, sort)


def table_string(rows):
Expand Down Expand Up @@ -258,7 +276,7 @@ def main():
help='specify a file path to get the hosts from')

ap.add_argument(
'-d', '--duration', type=int, default=DEFAULT_DURATION, metavar='secs',
'-d', '--duration', type=float, default=DEFAULT_DURATION, metavar='secs',
help='the duration how long the progress lasts (default: {})'.format(DEFAULT_DURATION))

ap.add_argument(
Expand Down Expand Up @@ -318,6 +336,7 @@ def main():

if not args.quiet:
print('\nFinal Results:\n')

print(results_string(results))


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setup(
name='mping',
version='0.1.1',
version='0.1.2',
packages=find_packages(),
package_data={
'': ['*.md'],
Expand Down

0 comments on commit fe2ac20

Please sign in to comment.