Advertisement
SirMongoose

Untitled

Feb 21st, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. import os, signal, threading
  2.  
  3. class WatchDog():
  4.     def __init__(self, timeout=5):
  5.         self.timeout = timeout
  6.         self._t = None
  7.  
  8.     def do_expire(self):
  9.         hostname = "8.8.8.8"
  10.         response = os.system("ping -c 1 " + hostname)
  11.         if response == 0:
  12.             self.refresh()
  13.         else:
  14.             print("Pinging 8.8.8.8 yielded no response!")
  15.             hostname = "8.8.4.4"
  16.             response = os.system("ping -c 1 " + hostname)
  17.             if response == 0:
  18.                 self.refresh()
  19.             else:
  20.                 print("Pinging 8.8.4.4 yielded no response!")
  21.  
  22.     def _expire(self):
  23.         print("\nWatchdog expire")
  24.         self.do_expire()
  25.  
  26.     def start(self):
  27.         if self._t is None:
  28.             self._t = threading.Timer(self.timeout, self._expire)
  29.             self._t.start()
  30.  
  31.     def stop(self):
  32.         if self._t is not None:
  33.             self._t.cancel()
  34.             self._t = None
  35.  
  36.     def refresh(self):
  37.         if self._t is not None:
  38.              self.stop()
  39.              self.start()
  40.  
  41. wd = WatchDog()
  42. wd.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement