Created
September 18, 2019 12:07
-
-
Save ghsatpute/7df3256fcd7114116473b5ddb0a0c54f to your computer and use it in GitHub Desktop.
Python script to check if Internet connectivity exists
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 | |
import logging | |
try: | |
# see if we can resolve the host name -- tells us if there is | |
# a DNS listening | |
host = socket.gethostbyname('www.google.com') | |
# connect to the host -- tells us if the host is actually | |
# reachable | |
s = socket.create_connection((host, 80), 2) | |
s.close() | |
logging.info('Internet connectivity is present') | |
except Exception as e: | |
logging.error('Internet connectivity is not present: {}'.format(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment