Run and log the output of 'ping' in a loop in a Python script
Question: i wrote a simple agaent in python that all it dose is just cheacks for the
internet connection. When he finds out that ther is no connection he writes a
log file to a text the hour and date and then just exit the program.

I want it to keep testing if there is a connection even if there is not how
can i do this ? without the program exit

this is the code:

    
    
    import os
    import time
    def Main():
    
        ping =os.system('ping -n 1 -l 1000 8.8.8.8 ')
        while ping ==0:
            time.sleep(4)
            ping = os.system('ping -n 1 -l 1000 8.8.8.8 ')
            if ping ==1:
                print 'no connection'
                CT =time.strftime("%H:%M:%S %d/%m/%y")
                alert=' No Connection'
                f = open('logfile.txt','a+')
                f.write('\n'+CT)
                f.write(alert)
                f.close()
    
    
    
    if __name__ == "__main__":
        Main()
    

Thanx a lot.


Answer: Wrap the `Main` call in an _infinite loop_?

    
    
    if __name__ == "__main__":
        while True:
            Main() 
            time.sleep(1) # optional, as Main already contains a sleep time
    

