How to check if the network is connected
通过判断请求百度谷歌首页成功与否,即可确认本地主机是否已接入网络。
import urllib.request
def network_check(host='https://www.baidu.com'):
try:
r = urllib.request.urlopen(host)
return True
except:
return False
print('网络已连接!' if network_check() else '网络未连接!')