Created
January 21, 2016 02:47
-
-
Save jwhitlock/7e513d82913cc1ba3adc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import logging | |
| import socket | |
| import time | |
| from urllib2 import urlopen, URLError, HTTPError | |
| from AppKit import CWInterface | |
| starttime=time.time() | |
| logger = logging.getLogger() | |
| def reConnect(): | |
| iface = CWInterface.interface() | |
| iface.setPower_error_(False, None) | |
| iface.setPower_error_(True, None) | |
| logger.info("Your wifi should work now!") | |
| def wifiCorrect(): | |
| socket.setdefaulttimeout( 1 ) | |
| url = 'http://google.com/' | |
| try : | |
| response = urlopen( url ) | |
| except HTTPError, e: | |
| logger.error("The server couldn't fulfill the request.", exc_info=True) | |
| except URLError, e: | |
| logger.error("We failed to reach a server.", exc_info=True) | |
| reConnect() | |
| except socket.timeout as e: | |
| logger.warning("Socket timeout.", exc_info=True) | |
| reConnect() | |
| else : | |
| logger.debug('got response!') | |
| if __name__ == "__main__": | |
| logging.basicConfig( | |
| level=logging.DEBUG, | |
| format="%(asctime)-15s %(message)s") | |
| while True: | |
| wifiCorrect() | |
| time.sleep(10.0 - ((time.time() - starttime) % 10.0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment