使用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模块:

[python]
inttime = time()##得到的是当前时间的小数形式:2881064151
structtime = localtime(inttime)###转换为本地时间,
#返回的结果是:time.struct_time(tm_year=2013, tm_mon=4, tm_mday=19,
inttime = time()##得到的是当前时间的小数形式:1366356992.617
structtime = localtime(inttime)###转换为本地时间,
#返回的结果是:time.struct_time(tm_year=2013, tm_mon=4, tm_mday=19, [python] view plaincopyprint?
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)
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)[python] view plaincopyprint?
##返回的就是你想要的格式的字符串:2013-04-19,15:36:32

##返回的就是你想要的格式的字符串: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 当前时区的名称
%% %号本身

time模块目录下自己建立一个名为log的文件夹的更多相关文章

  1. 代码实现:判断E盘目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称

    package com.loaderman.test; import java.io.File; import java.io.FilenameFilter; public class Test { ...

  2. 一个diff工具,用于判断两个目录下所有的改动(比较新旧版本文件夹)

    需求: 编写一个diff工具,用于判断两个目录下所有的改动 详细介绍: 有A和B两个目录,目录所在位置及层级均不确定 需要以B为基准找出两个目录中所有有改动的文件(文件或内容增加.修改.删除),将有改 ...

  3. 如何在linux系统下对文件夹名有空格的文件夹进行操作

    http://www.2cto.com/os/201409/335119.html 在Windows操作系统中可以轻易地创建\移动\删除文件夹名带有空格的文件夹, 而在linux则需要进行一些特殊的处 ...

  4. matlab从文件夹名中获得该文件夹下所图像文件名

    function [s,nameC]=get_FileNameFromFolderPath(path) % 函数调用:[s,nameC]=get_FileNameFromFolderPath(path ...

  5. Windows下使用图形化mount挂载磁盘到文件夹

    Windows上也有类似于Linux上的mount命令,至于mount是什么: mount是Linux下的一个命令,它可以将分区挂接到Linux的一个文件夹下,从而将分区和该目录联系起来,因此我们只要 ...

  6. Windows 10 下安装 npm 后全局 node_modules 和 npm-cache 文件夹的设置

    npm 指 Node Package Manager,是 Node.js 中一个流行的包管理和分发工具.Node.js 在某个版本的 Windows 安装包开始已经加入了 npm,现在可以进入 htt ...

  7. VS下如何建立一个新的MFC程序 网络编程 课设 基于C++ MFC 连接数据库 小应用 小项目浅析展示

    原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/8191036.html 这里不知道会不会有人是真的新手 新新手 不知道怎么 如何建立一个MFC ...

  8. 删除windows上特定目录下以*.rar后缀名的python脚本

    import os,fnmatch,datetime,time def all_files(root,pattern='*',single_level=False,yield_folders=Fals ...

  9. 用Python删除本地目录下某一时间点之前创建的所有文件

    因为工作原因,需要定期清理某个文件夹下面创建时间超过1年的所有文件,所以今天集中学习了一下Python对于本地文件及文件夹的操作.网上 这篇文章 简明扼要地整理出最常见的os方法,抄袭如下: os.l ...

随机推荐

  1. Jmeter分布式测试搭建(二)

    Jmeter运行的时候十分耗内存和cpu,跑到500多个进程的时候,就卡死了.我们测试时,如果进行大数据量的并发测试时,单个电脑的CPU和内存可能无法承受,这个时候,我们需要进行一个分布式的测试,比如 ...

  2. JS获得鼠标位置

    <body> <script> function mouseMove(ev) { ev = ev || window.event; var mousePos = mouseCo ...

  3. express-16 与生产相关的问题2

    处理未捕获的异常 在Node的异步世界中,未捕获的异常是特别需要关注的问题 app.get('/fail', function(req, res){ throw new Error('Nope!'); ...

  4. Fzu月赛11 老S的旅行计划 dij

    Description 老S在某城市生活的非常不自在,想趁着ICPC举办期间在省内转转.已知老S所在的省有N个城市,M条无向边(对于某一对结点可能出现重边).由于省内的交通相当糟糕,通过某条边所需要花 ...

  5. kafka 集群安装与安装测试

    一.集群安装 1. Kafka下载:wget https://archive.apache.org/dist/kafka/0.8.1/kafka_2.9.2-0.8.1.tgz 解压 tar zxvf ...

  6. xampp的Apache无法启动解决方法

    XAMPP Apache 无法启动原因1(缺少VC运行库): 这个就是我遇到的问题原因,下载安装的XAMPP版本是xampp-win32-1.7.7-VC9,而现有的Windows XP系统又没有安装 ...

  7. ural 1338. Automobiles

    1338. Automobiles Time limit: 3.0 secondMemory limit: 64 MB Everyone knows what traffic congestion i ...

  8. IO中同步、异步与阻塞、非阻塞的区别

    一.同步与异步同步/异步, 它们是消息的通知机制 1. 概念解释A. 同步所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回. 按照这个定义,其实绝大多数函数都是同步调用(例如si ...

  9. 桌面每日一句--桌面翻译工具(有道翻译,微软翻译,Google翻译)

    现在的翻译软件功能越来越多,也越来越臃肿,还不时弹广告,真的很烦恼. 鉴于这种情况,自己做了个翻译软件,能满足日常需求就好了.需要用的时候可以直接在桌面输入单词翻译,或者直接使用快捷键呼出翻译窗口.最 ...

  10. Leetcode ReorderList

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...