我想看看我是否可以访问在线API,但为了实现这一点,我需要有Internet访问权限。
如何使用Python查看是否有可用的活动连接?
发布于 2015-09-04 12:45:56
import urllib
def connected(host='http://google.com'):
try:
urllib.urlopen(host)
return True
except:
return False
# test
print( 'connected' if connected() else 'no internet!' )对于python 3,使用urllib.request.urlopen(host)
https://stackoverflow.com/questions/3764291
复制相似问题