现在的位置: 主页 > 企业简介 > 文章列表

python下使用ping检查网络连通情况

作者:程力专用汽车股份有限公司 来源:www.dfclxwc.com 未知发布时间:2017-09-05 12:10:06
python下使用ping检查网络连通情况

使用python调用ping命令,然后在日志中记录ping的结果,用来监测网络连通情况。

代码:


[python]
from time import *
from subprocess import *


webf= open("webs.txt","r")
webs=[]
for w in webf:
webs.append(w.strip())

def logAppend(log,info):
inttime = time()
structtime = localtime(inttime)
strtime = strftime("%Y-%m-%d,%H:%M:%S",structtime)
print "at ",strtime

log.write("================== "+strtime+" ==================\n")
log.write(info)
log.write("\n\n")
print "append info to file :",log.name
print info


def netCheck():
while True:
for url in webs:
p = Popen(["ping.exe",url],
stdin=PIPE,stdout=PIPE,stderr=PIPE,
shell=True)
out = p.stdout.read()
log = open("log\\"+url+".log","a")

logAppend(log,out)
log.close()
sleep(0.01)
print "waiting ..."
sleep(60*15) #sleep for 15min. 60*15
return

def main():
"""
the main function
"""
print "start..."
netCheck()
print "end."


if __name__ == "__main__":
main()

from time import *
from subprocess import *


webf= open("webs.txt","r")
webs=[]
for w in webf:
webs.append(w.strip())

def logAppend(log,info):
inttime = time()
structtime = localtime(inttime)
strtime = strftime("%Y-%m-%d,%H:%M:%S",structtime)
print "at ",strtime

log.write("================== "+strtime+" ==================\n")
log.write(info)
log.write("\n\n")
print "append info to file :",log.name
print info


def netCheck():
while True:
for url in webs:
p = Popen(["ping.exe",url],
stdin=PIPE,stdout=PIPE,stderr=PIPE,
shell=True)
out = p.stdout.read()
log = open("log\\"+url+".log","a")

logAppend(log,out)
log.close()
sleep(0.01)
print "waiting ..."
sleep(60*15) #sleep for 15min. 60*15
return

def main():
"""
the main function
"""
print "start..."
netCheck()
print "end."


if __name__ == "__main__":
main()

说明:

webs.txt为目的地址,如www.baidu.com,站群系统,每行一个。

需要在当前目录下自己建立一个名为log的文件夹。

关于time模块:

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:襄阳SEO http://xiangyang.4567w.com

上一篇:Python学习练习5----字典、列表、函数的运用 下一篇:最后一页