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
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