python 实现判断ip连通性的方法总结

yipeiwu_com7年前Python基础

python

以下是个人学习 python 研究判断ip连通性方法的集合。 缺点可能有办法解决,如有错误,欢迎矫正。

方法一

import os
return1=os.system('ping -n 2 -w 1 172.21.1.183')
print return1

缺点:会弹出cmd 窗口

方法二

#-*- coding: utf-8 -*-
import subprocess
import re
p = subprocess.Popen(["ping.exe ", '172.21.183.183'],stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE,shell = True)
out = p.stdout.read()
print out
regex = re.compile("Minimum = (\d+)ms, Maximum = (\d+)ms, Average = (\d+)ms", re.IGNORECASE)
print regex.findall(out)

缺点: 默认ping 4次 暂时没有找到 控制ping次数的方法

方法三

from subprocess import call
result = call("ping 172.21.4.20 -n 1",shell=True)
print result

缺点,好像不太靠谱

方法四 这个方式应该是linux下的调用,没试过

import os,sys,re
import subprocess
p = subprocess.Popen(["ping -c 1 -w 1 172.21.183.183"],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
out=p.stdout.read()
err=p.stderr.read()
regex=re.compile('100% packet loss')
print out

以上这篇python 实现判断ip连通性的方法总结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

可用于监控 mysql Master Slave 状态的python代码

复制代码 代码如下:import osimport sysimport MySQLdbdef getStatus(conn):    query = " S...

python实现海螺图片的方法示例

python实现海螺图片的方法示例

本文介绍了如何用python实现海螺图片,感兴趣的可以参考一下,具体代码如下: 代码如下: import turtle import time t = turtle.Turtl...

python画一个玫瑰和一个爱心

节日用心准备的礼物,使用python画玫瑰和爱心,供大家参考,具体内容如下 #!/usr/bin/env python #coding=utf-8 #女生节礼物 import r...

python 中Arduino串口传输数据到电脑并保存至excel表格

python 中Arduino串口传输数据到电脑并保存至excel表格

起因:学校运河杯报了个项目,制作一个天气预测的装置。我用arduino跑了BME280模块,用蓝牙模块实现两块arduino主从机透传。但是为了分析,还需要提取出数据。因此我用pytho...

浅谈Django中的数据库模型类-models.py(一对一的关系)

如下所示: # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import...