Created
December 16, 2011 01:46
-
-
Save irachex/1484023 to your computer and use it in GitHub Desktop.
Restart mDNSResponder if it's not responsible (just for MacOS) Run this script as root via cron.
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/env python | |
""" Restart mDNSResponder if it's not responsible (just for MacOS) | |
Run this script as root via cron. | |
* * * * * /Users/xupeng/bin/check_mDNSResponder.py | |
""" | |
import os | |
import sys | |
import threading | |
import socket | |
import Queue | |
def check_dns(hostname, timeout): | |
""" Do DNS resolving with given timeout | |
return IP address if success before timeout, raise socket.timeout if timeout. | |
""" | |
def proc(): | |
try: | |
queue.put(socket.gethostbyname(hostname)) | |
except Exception, e: | |
raise e | |
queue = Queue.Queue() | |
t = threading.Thread(target=proc) | |
t.daemon = True | |
t.start() | |
try: | |
return queue.get(timeout=timeout) | |
except Queue.Empty: | |
raise socket.timeout | |
def main(): | |
try: | |
check_dns('www.douban.com', 1) | |
return 0 | |
except socket.timeout: | |
os.system('/usr/bin/killall -9 mDNSResponder') | |
return 1 | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment