#!/usr/bin/env python#-*- coding: utf-8 -*-#@Time : 2019-04-19 09:50#@Author : yuyongxr#@Site :#@File : network_testing_v0.1.py#@Software: PyCharm#
from icmp_test import *
from http_test import *
from config import *
importthreadingimporttimeimportjson
http_status=Truedefstart_check_http(url,title):globalhttp_statuswhileTrue:ifcheck_http(url,title):
http_status=True#print("http_status :", http_status)
else:
http_status=Falseprint("http_status :", http_status)
file= open("http_logs.txt", "a")
file.write(time.asctime(time.localtime(time.time()))+ ": http status is" +str(http_status) + "\n")
time.sleep(1)defcheck_continuous():
url,title,ip_address,times,domain,dnsserver=analyze_config()
t_http= threading.Thread(target=start_check_http,args=(url,title))
t_http.start()deficmp_continuous():globalhttp_statusglobalavg_listgloballost_list
avg_list=[]
lost_list=[]
url,title,ip_address,times,domain,dnsserver=analyze_config()whileTrue:ifhttp_status:
min, avg, max, lost= "","","",""
try:
min, avg, max, lost=shend_icmp_packet(ip_address, times)
avg_flag=0
avg_list.append(float(avg))for a inavg_list:
avg_flag= avg_flag +a
avg_flag= avg_flag/len(avg_list)
lost_flag=0
lost_list.append(float(lost))for a inlost_list:
lost_flag= lost_flag +a
lost_flag= lost_flag /len(lost_list)
file= open("result.json")
content=file.read()
file.close()
content=json.loads(content)
content["ping"]["min"] =float(content["ping"]["min"]) if float(content["ping"]["min"]) <= float(min) elsemin
content["ping"]["avg"] =avg_flag
content["ping"]["max"] = float(content["ping"]["max"]) if float(content["ping"]["max"]) >= float(max) elsemax
content["ping"]["lost"] =lost_flag
content=json.dumps(content)
file= open("result.json","w")
file.write(content)
file.close()print(min, avg, max, lost)except:pass
else:
time.sleep(1)continuetime.sleep(5)defmain():
c_check= threading.Thread(target=check_continuous)
i_icmp= threading.Thread(target=icmp_continuous)
c_check.start()
i_icmp.start()if __name__ == '__main__':
main()