获取ping的最短、最长、平均时间

# -*- coding: utf-8 -*-

import os
import re
p = os.popen('ping 120.26.77.101')

out = p.read()
regex = re.compile("\xd7\xee\xb6\xcc = (\d+)ms\xa3\xac\xd7\xee\xb3\xa4 = (\d+)ms\xa3\xac\xc6\xbd\xbe\xf9 = (\d+)ms", re.IGNORECASE)
all_result = regex.findall(out)
min_time = int(all_result[0][0])
max_time = int(all_result[0][1])
avg_time = int(all_result[0][2])
print('最短时间:' + str(min_time) + 'ms, ' + '最长时间:' + str(max_time) + 'ms, ' + '平均时间:' + str(avg_time) + 'ms')

 

 


import subprocess    
import re
p = subprocess.Popen(["ping.exe", 'google.com'], 
                                         stdin = subprocess.PIPE, 
                                         stdout = subprocess.PIPE, 
                                         stderr = subprocess.PIPE, 
                                         shell = True)    

out = p.stdout.read()                                     
regex = re.compile("Minimum = (\d+)ms, Maximum = (\d+)ms, Average = (\d+)ms", re.IGNORECASE)
print regex.findall(out)

posted on 2015-05-14 10:56  帅胡  阅读(1476)  评论(0编辑  收藏  举报

导航