Skip to content

Instantly share code, notes, and snippets.

@ghsatpute
Created September 18, 2019 12:07
Show Gist options
  • Save ghsatpute/7df3256fcd7114116473b5ddb0a0c54f to your computer and use it in GitHub Desktop.
Save ghsatpute/7df3256fcd7114116473b5ddb0a0c54f to your computer and use it in GitHub Desktop.
Python script to check if Internet connectivity exists
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