python下使用ping检查网络连通情况
2013-04-19 15:37
363 查看
使用python调用ping命令,然后在日志中记录ping的结果,用来监测网络连通情况。
代码:
说明:
webs.txt为目的地址,如www.baidu.com,每行一个。
需要在当前目录下自己建立一个名为log的文件夹。
关于time模块:
其他参数类型:
strftime(format[, tuple]) -> string
将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出
python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
代码:
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模块:
inttime = time()##得到的是当前时间的小数形式:1366356992.617 structtime = localtime(inttime)###转换为本地时间, #返回的结果是:time.struct_time(tm_year=2013, tm_mon=4, tm_mday=19,
tm_hour=15, tm_min=36, tm_sec=32, tm_wday=4, tm_yday=109, tm_isdst=0) #这个看着很不顺眼,继续格式化转换: strtime = strftime("%Y-%m-%d,%H:%M:%S",structtime)
##返回的就是你想要的格式的字符串:2013-04-19,15:36:32
其他参数类型:
strftime(format[, tuple]) -> string
将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出
python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
相关文章推荐
- c#如何检查局域网内服务器是否连通(调用外部Ping命令获取网络连接情况)
- c#如何检查局域网内服务器是否连通(调用外部Ping命令获取网络连接情况)
- c#如何检查局域网内服务器是否连通(调用外部Ping命令获取网络连接情况)
- 使用C#调用外部Ping命令获取网络连接情况
- 使用C#调用外部Ping命令获取网络连接情况
- 使用C#调用外部Ping命令获取网络连接情况
- python windows下通过SSH获取linux系统cpu、内存、网络使用情况
- 使用PING和Traceroute 检查网络的连通性
- 使用C#调用外部Ping命令获取网络连接情况
- 使用C#调用外部Ping命令获取网络连接情况
- 使用C#调用外部Ping命令获取网络连接情况
- 使用C#调用外部Ping命令获取网络连接情况
- 使用C#调用外部Ping命令获取网络连接情况(转——主要是对dos命令应用)
- 使用C#调用Ping命令获取网络连接情况 + 获取DOS返回值
- go 语言使用ssh包连接操作Linux服务器,使用ping包查看服务器网络是否正常,使用cron包定时查询该服务器CPU使用情况
- [Python]网络爬虫学习笔记,爬取东南大学SEU-wlan的流量使用情况
- 使用C#调用外部Ping命令获取网络连接情况[转载CSDN]
- 使用PING和Traceroute 检查网络的连通性
- 使用C#调用Ping命令获取网络连接情况(C#命令行交互)
- 使用C#调用外部Ping命令获取网络连接情况