I'm running a small python script to check every 10 seconds to see if my internet access is available (been having problems with my isp sucking). I've been running it for probably 2 months and it's worked perfectly, but now it randomly exits. Sometimes it exits within 20 seconds of me starting it, and sometimes it waits 5 minutes. the code is:
import time
import datetime
import urllib2
waitTime = 300
outfile = "C:\Users\simmons\Desktop\internetConnectivity.txt"
def internetOffline():
with open (outfile, "a") as file:
file.write("Internet Offline: %s\n" % time.ctime())
print("Internet Went Down!")
def internetCheck():
try:
urllib2.urlopen('https://www.google.com', timeout = 2)
except urllib2.URLError:
internetOffline()
while (1):
internetCheck()
time.sleep( 10 )
My question is not only, how would I print out what is happening when it exits, but also, does anyone know of a more efficient way of doing this, so it possibly causes less network traffic. It's not a problem now, but I was just wondering about more efficient methods.