Python3笔记之网络连通检测

代码

import subprocess,time,random

def ynShell(command):

    subp = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding="utf-8")
    subp.wait(10)
    if subp.poll() == 0:
        print('网络通畅')
        # print(subp.communicate())
        # print(subp.returncode)
        return True
    else:
        print("网络不通畅")
        return False

if __name__ == '__main__':

    _list = [
        'ping www.xx.xx',
        'ping www.baidu.com'
    ]
    print('======= 网络通畅检测 =======')
    while True:
        ynShell(random.choice(_list))
        time.sleep(5)

你可能感兴趣的:(Python3,python,网络)