python check network connection code example
Example 1: python check if internet is available
import urllib.request
def internet_on():
try:
urllib.request.urlopen('http://216.58.192.142', timeout=2)
return True
except:
return False
Example 2: python check internet connection
import urllib2
def internet_on():
try:
urllib2.urlopen('http://216.58.192.142', timeout=1)
return True
except urllib2.URLError as err:
return False