1import urllib.request
2
3def internet_on():
4 try:
5 urllib.request.urlopen('http://216.58.192.142', timeout=2)
6 return True
7 except:
8 return False
1try:
2 import httplib
3except:
4 import http.client as httplib
5
6def have_internet():
7 conn = httplib.HTTPConnection("www.google.com", timeout=5)
8 try:
9 conn.request("HEAD", "/")
10 conn.close()
11 return True
12 except:
13 conn.close()
14 return False
15# Code by Ivelin on Stack overflow
1import urllib2
2
3def internet_on():
4 try:
5 urllib2.urlopen('http://216.58.192.142', timeout=1)
6 return True
7 except urllib2.URLError as err:
8 return False