本文實例講述了Python測試網絡連通性。分享給大家供大家參考,具體如下:
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 以太網 disable") if result == 1: print("disable network card failed") else: print("disable network card successfully")def ping(): ''' ping 主備網絡 ''' result = os.system(u"ping 180.97.33.108") #result = os.system(u"ping www.baidu.com -n 3") if result == 0: print("A網正常") else: print("網絡故障") return resultif __name__ == '__main__': while True: PING_RESULT = ping() if PING_RESULT == 0: time.sleep(20) else: DisableNetwork() time.sleep(10)
運行結果:
注:原文為utf-8
編碼,這里小編測試時發現返回結果會出現亂碼,故改為GBK
編碼。
更多關于Python相關內容可查看本站專題:《Python Socket編程技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答