Use Ping in Python to check network connectivity

Source: Internet
Author: User
Tags month name

Use python to call the ping command and record the ping result in the log to monitor network connectivity.

Code:

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 infodef 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    returndef main():    """    the main function    """    print "start..."    netCheck()    print "end."if __name__ == "__main__":    main()

Note:

For example, www.baidu.com.

Create a folder named log in the current directory.

About the time module:

Inttime = Time () # the decimal form of the current time is obtained: 1366356992.617 structtime = localtime (inttime) ### convert to local time, # The returned result is: time. struct_time (maid = 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) # This is not pleasing to the eye, continue formatting and conversion: strtime = strftime ("% Y-% m-% d, % H: % m: % s", structtime)
# The returned result is the string in the desired format: 2013-04-19,15: 36: 32

Other parameter types:

Strftime (Format [, tuple])-> string
Returns the specified struct_time (current time by default) according to the specified formatted string.
Time and date formatting symbols in Python:
% Y two-digit year (00-99)
% Y indicates the four-digit year (000-9999)
% M month (01-12)
One day in % d month (0-31)
% H hour in 24-hour format (0-23)
% I 12-hour (01-12)
% M minutes (00 = 59)
% S seconds (00-59)

% A local simplified week name
% A local full week name
% B local simplified month name
% B local full month name
% C local Date and Time
% J one day in the year (001-366)
% P local equivalent of a. m. or P. M.
% U number of weeks in a year (00-53) Sunday is the start of the week
% W Week (0-6), Sunday is the beginning of the week
% W number of weeks in a year (00-53) Monday is the start of the week
% X local date Representation
% X Local Time Representation
% Z Current Time Zone name
% Itself


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.