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

2020-01-09 12:31:00 浏览数 (1)

代码语言:javascript复制
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()

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

0 人点赞