Skip to content

Instantly share code, notes, and snippets.

@LiamDobbelaere
Last active August 1, 2018 13:20
Show Gist options
  • Save LiamDobbelaere/d64b35942adc512218ecd63581e4b35b to your computer and use it in GitHub Desktop.
Save LiamDobbelaere/d64b35942adc512218ecd63581e4b35b to your computer and use it in GitHub Desktop.
Internet connection logging script for Python 27
import time
import datetime
try:
import httplib
except:
import http.client as httplib
def have_internet():
conn = httplib.HTTPConnection("www.google.com", timeout=5)
try:
conn.request("HEAD", "/")
conn.close()
return True
except:
conn.close()
return False
with open("internet.log", "a") as myfile:
myfile.write("[" + str(datetime.datetime.now()) + "] Logging started\n")
while True:
if have_internet():
print "[" + str(datetime.datetime.now()) + "] OK"
else:
with open("internet.log", "a") as myfile:
myfile.write("[" + str(datetime.datetime.now()) + "] No connection!\n")
print "[" + str(datetime.datetime.now()) + "] No connection! Logging."
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment