本文實(shí)例講述了Python測(cè)試網(wǎng)絡(luò)連通性。分享給大家供大家參考,具體如下:
Python代碼
#!/usr/bin/python# -*- coding:GBK -*-"""Document: network script, keep network always working, using python3"""import osimport timePING_RESULT = 0NETWORK_RESULT = 0def DisableNetwork(): ''' disable network card ''' result = os.system(u"netsh interface set interface 以太網(wǎng) disable") if result == 1:  print("disable network card failed") else:  print("disable network card successfully")def ping(): ''' ping 主備網(wǎng)絡(luò) ''' result = os.system(u"ping 180.97.33.108") #result = os.system(u"ping www.baidu.com -n 3") if result == 0:  print("A網(wǎng)正常") else:  print("網(wǎng)絡(luò)故障") return resultif __name__ == '__main__': while True:  PING_RESULT = ping()  if PING_RESULT == 0:   time.sleep(20)  else:   DisableNetwork()   time.sleep(10)運(yùn)行結(jié)果:
	注:原文為utf-8編碼,這里小編測(cè)試時(shí)發(fā)現(xiàn)返回結(jié)果會(huì)出現(xiàn)亂碼,故改為GBK編碼。
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選