代码很简单,需要设置能访问的url即可
import socket
def is_connected(url):
try:
host = socket.gethostbyname(url)
s = socket.create_connection((host, 80), 2)
return True
except Exception as e:
return False
URL = "www.baidu.com"
print is_connected(URL )