python使用urllib测试网络的连通性(判断网络是否可用)

 

from urllib import request
import time


def check_net(testserver):
    try:
        ret = request.urlopen(url=testserver, timeout=3.0)
        # print(ret)
    except:
        return False
    return True


t1 = time.time()
print(check_net('https://www.baidu.com'))
t2 = time.time()
print('check_net spend time', t2 - t1)

  

 

posted @ 2020-05-11 15:16  平平无奇小辣鸡  阅读(2162)  评论(0编辑  收藏  举报