Skip to content

Instantly share code, notes, and snippets.

@atvKumar
Created April 17, 2014 03:10
Show Gist options
  • Save atvKumar/10950273 to your computer and use it in GitHub Desktop.
Save atvKumar/10950273 to your computer and use it in GitHub Desktop.
Checks if internet connection is on.
import socket
def check_net(address='google.com', debug=False):
"""
Checks if internet connection to address given is valid and working
Defaults to using 'google.com' in an event nothing is passed
Returns : True if resolved and False if not resolved
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect((address, 1)) # Changed from 0
return True
except socket.gaierror, err:
if debug:
print('Failed to Resolve {0}'.format(address), err)
return False
finally:
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment