python代码来判断现在是否连接网络

#!/usr/bin/env python
#coding:utf-8
 
def test1():
    import os
    return1=os.system('ping 8.8.8.8')
    if return1:
        print 'ping fail'
    else:
        print 'ping ok'
         
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:
        print 'ping fail'
    else:
        print 'ping ok'
    fnull.close()
 
if __name__=='__main__':
    test2()
    

你可能感兴趣的:(python代码来判断现在是否连接网络)