Skip to content

Instantly share code, notes, and snippets.

@irachex
Created December 16, 2011 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irachex/1484023 to your computer and use it in GitHub Desktop.
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.
#!/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