Home  >  Article  >  Backend Development  >  python在windows下实现ping操作并接收返回信息的方法

python在windows下实现ping操作并接收返回信息的方法

WBOY
WBOYOriginal
2016-06-06 11:22:151697browse

本文实例讲述了python在windows下实现ping操作并接收返回信息的方法。分享给大家供大家参考。具体分析如下:

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

代码如下:

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)

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn