1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import os
def ping(adresse, repeat=1, data_size=32, TTL=255):
commande = "ping %s -n %i -l %i -i %i" % (adresse, repeat, data_size, TTL)
r = os.popen(commande).read()
temps = []
for ligne in r.split('\n'):
for t in ["temps=", "temps<"]:
try:
temps.append(int(ligne.split(t)[1].split('ms')[0].strip()))
except:
pass
else:
break
return temps
if __name__ == '__main__':
print "ping localhost: %i ms" % ping('127.0.0.1', repeat=1).pop()
print "ping google.fr: %s" % str(ping('google.fr', repeat=6)) |
Partager