Sam*_*phy 1 networking python-3.x
如何检查 python3 模块是否连接到互联网?
我看到这是Python2 的解决方案,并想做类似的事情。
以下片段对我有用。
from urllib.request import urlopen
def internet_on():
try:
response = urlopen('https://www.google.com/', timeout=10)
return True
except:
return False
Run Code Online (Sandbox Code Playgroud)
其他用例可能需要改进的错误处理和/或其他配置。