User-Profile-Image
hankin
  • 5
  • 首页
  • 分类
    • Windows
    • Web
    • VoIP
    • Virtualization
    • Trading
    • Smart Home
    • SEO
    • Security
    • Python
    • Program
    • PHP
    • OSX
    • OS
    • OpenSource
    • Nginx
    • MySQL
    • Mood
    • Linux
    • Life
    • Injection
    • Hosting
    • Finance
    • Exploits
    • CDN
  • 页面
    • About
  • 友链
    • 维酷科技
    • Tesra超算网络
Help?

Please contact us on our email for need any support

Support
  • 首页
    首页   ›   Program   ›   Python   ›   正文
Python

Python实现Ping

2010-10-23 21:47:02
2113  0 0

这里使用的是最简易的方式,使用python的子进程管理模块,调用系统的ping命令,代码如下:

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)

ping的结果用正则取出来。

该代码在xp+python2.5上测试通过,如果是linux系统,需要修改正则,因为linux上的ping的返回结果和windows的不太一样。该短代码的缺点是调用的时候,由于要等待ping命令返回,所以界面会假死,可以考虑开单独的线程处理。

评论 (0)

Click here to cancel reply.

欢迎您 游客  

    ProdigYu
    180文章 77评论 4点赞 553988浏览
    随机文章
    PHP显示500错误详细信息一句话方法
    9年前
    CentOS7\Ubuntu安装VMware Workstation 12
    9年前
    N2N中心节点的编译和部署
    9年前
    自主架设VOIP系统
    15年前
    CentOS安装VNC发生一个奇葩的错误
    11年前
    Tags
    Apache CentOS CMD DDOS Debian DNS Docker FastCGI Google IIS IP iptables Linux Mac MySQL Nginx Opensource PHP Python Skill SSH Ubuntu Vim VMware VPN web.py Windows 删除 命令 命令提示符 安装 开源 技巧 插件 漏洞 百度 系统 组件 终端 缓存 网络电话 脚本 自建 详解 错误
    Meta
    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org
    Copyright © 2025
    主页
    页面
    • About
    博主
    ProdigYu 管理员
    Something about myself
    180 文章 77 评论 553988 浏览
    测试
    测试