python 判断网络状态_python 判断网络通断同时检测网络的状态

#!/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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值