Python測試網(wǎng)絡(luò)連通性示例【基于ping】
本文實(shí)例講述了Python測試網(wǎng)絡(luò)連通性。分享給大家供大家參考,具體如下:
Python代碼
#!/usr/bin/python
# -*- coding:GBK -*-
"""Document: network script, keep network always working, using python3"""
import os
import time
PING_RESULT = 0
NETWORK_RESULT = 0
def 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 result
if __name__ == '__main__':
while True:
PING_RESULT = ping()
if PING_RESULT == 0:
time.sleep(20)
else:
DisableNetwork()
time.sleep(10)
運(yùn)行結(jié)果:

注:原文為utf-8編碼,這里小編測試時(shí)發(fā)現(xiàn)返回結(jié)果會(huì)出現(xiàn)亂碼,故改為GBK編碼。
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python(Django)項(xiàng)目與Apache的管理交互的方法
這篇文章主要介紹了Python(Django)項(xiàng)目與Apache的管理交互的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
tensorflow入門:tfrecord 和tf.data.TFRecordDataset的使用
今天小編就為大家分享一篇tensorflow入門:tfrecord 和tf.data.TFRecordDataset的使用,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
在django中實(shí)現(xiàn)頁面倒數(shù)幾秒后自動(dòng)跳轉(zhuǎn)的例子
今天小編就為大家分享一篇在django中實(shí)現(xiàn)頁面倒數(shù)幾秒后自動(dòng)跳轉(zhuǎn)的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python使用matplotlib繪制余弦的散點(diǎn)圖示例
這篇文章主要介紹了Python使用matplotlib繪制余弦的散點(diǎn)圖,涉及Python操作matplotlib的基本技巧與散點(diǎn)的設(shè)置方法,需要的朋友可以參考下2018-03-03

