Created
April 17, 2014 03:10
-
-
Save atvKumar/10950273 to your computer and use it in GitHub Desktop.
Checks if internet connection is on.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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