Advertisement
mvineetmenon

Autoshutdown in no internet connectivity

Mar 30th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. from os import *
  2. import urllib.request
  3. import time
  4.  
  5. #A function for checking the connectiviy with internet.
  6. #Return: If connection establishes, a urlopen handle otherwise None.
  7. def testconnection():
  8.     try:
  9.         f = urllib.request.urlopen("http://www.google.com")
  10.     except:
  11.         print("NOT OK")
  12.     else:
  13.         print("OK")
  14.         return f
  15.    
  16.  
  17. while True:
  18.     ff = testconnection()
  19. #   check for the urlopen handle, if connection is not present follow this path,
  20.     if(ff == None):
  21.         print("No Internet Connection! Shutting Down System!")
  22. #       works only in Windows; TODO extend for linux as well
  23.         system("shutdown /s /t 100")
  24.         exit(0)
  25. #   If connection exist, then sleep for 15 minutes and loop back.
  26.     else:
  27.         print("InternetConnection Intact")
  28.         time.sleep(15*60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement