【Python】斷網檢測重新啓動客戶端程序

每個5秒檢測一次網絡,若斷網,則關閉上網客戶端並且重新打開。
需要在管理員權限下打開cmd運行。

# encoding: utf-8
import os
import time

def restart_Drc():
    command = 'taskkill /F /IM DrClient.exe'
    os.system(command)
    command = 'taskkill /F /IM DrMain.exe'
    os.system(command)
    command = 'start '' C:\Drcom\DrUpdateClient\DrMain.exe'
    os.system(command)

res = 0
while 1:
    print('')
    print('=======================正在檢測網絡重連=======================')
    print('')
    return1 = os.system('ping www.baidu.com')
    if return1:
        print('')
        print('=======================網絡異常,重啓客戶端=======================')
        print('')
        restart_Drc()
        res += 1
    else:
        print('')
        print('=======================網絡正常=======================')
        print('')

    print('=======================已經重連%s次=======================' % res)
    time.sleep(5)

Linux版,提前設置好開機自啓動後登陸聯網軟件,如果斷網的話直接重啓

# encoding: utf-8
import os
import time

def restart_Drc():
    command = 'reboot'
    os.system(command)


print('=======================等待43200秒開始檢測=======================')
time.sleep(43200)

while 1:
    print('')
    print('=======================正在檢測網絡重連=======================')
    print('')
    return1 = os.system('ping www.baidu.com -c5')
    if return1:
        restart_Drc()
    else:
        print('')
        print('=======================網絡正常=======================')
        print('')

    time.sleep(5)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章