python监测当前联网状态并连接

2023-01-31 03:01:11 联网 状态 监测
def test1():
    import os
    return1=os.system('ping 8.8.8.8')
    if return1:
        print 'ping fail'
        os.system('msdt.exe /id NetworkDiagnosticsNetworkAdapter')#调用系统网络诊断
    else:
        print 'ping'
def test2():
    import os
    import subprocess

    fnull = open(os.devnull, 'w')
    return1 = subprocess.call('ping 8.8.8.8', shell = True, stdout = fnull, stderr = fnull)
    if return1:
        return1 = subprocess.call('msdt.exe /id NetworkDiagnosticsNetworkAdapter', shell=True, stdout=fnull, stderr=fnull)
        print 'ping fail'
    else:
        print 'ping ok'
    fnull.close()

if __name__=='__main__':
    test2()


监测当前是否联网,没有时调用系统的联网监测;也算是原创吧,百度了下,功能都是分开的,综合到了一起.

相关文章