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)