Last active
August 1, 2018 13:20
-
-
Save LiamDobbelaere/d64b35942adc512218ecd63581e4b35b to your computer and use it in GitHub Desktop.
Internet connection logging script for Python 27
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 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