python在windows下实现ping操作并接收返回信息,pythonping,这段python代码调用


这段python代码调用windows下的ping命令,通过subprocess在其子进程里面实现,由于windows下的ping和linux下的ping返回的消息不太一样,所以这段python代码要想在linux下运行,需要修改一下正则匹配

import subprocess   import rep = 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)

评论关闭