import urllib.request
def internet_on():
try:
urllib.request.urlopen('http://216.58.192.142', timeout=2)
return True
except:
return False
try:
import httplib # python < 3.0
except:
import http.client as httplib
def have_internet():
conn = httplib.HTTPSConnection("8.8.8.8", timeout=5)
try:
conn.request("HEAD", "/")
return True
except Exception:
return False
finally:
conn.close()
try:
import httplib
except:
import http.client as httplib
def have_internet():
conn = httplib.HTTPConnection("www.google.com", timeout=5)
try:
conn.request("HEAD", "/")
conn.close()
return True
except:
conn.close()
return False
# Code by Ivelin on Stack overflow
# pip install pythonping
from pythonping import ping
def internet_connected(test_ip_address="8.8.8.8"):
return ping(test_ip_address).success()