0

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.

trueCamelType
  • 2,198
  • 5
  • 39
  • 76
  • You can try checking the error message it produces when it exits? – justhalf Sep 25 '13 at 02:26
  • If you just want to see some errors try `sys.stderr = open("error.txt", "w")` and check the file after a crash. Will need to be run with permissions probably. – Leonardo Sep 25 '13 at 02:36

1 Answers1

2

this could be from going to google to many times im not to sure

run the program in you're IDE and then read the error it throws on exit this should tell you what or where the program is exiting

Here is a good way to do this:

import urllib2

def internet_on():
    try:
        response=urllib2.urlopen('http://74.125.228.100',timeout=1)
        return True
    except urllib2.URLError as err: pass
    return False

74.125.228.100 is one of the IP-addresses for google.com. Change http://74.125.228.100 to whatever site can be expected to respond quickly

I got this solution from this question, take a look at it, it should help

Community
  • 1
  • 1
Serial
  • 7,925
  • 13
  • 52
  • 71