def __dns_resolve_host(host, ip_version, timeout):
"""
Resolve a host using the system's facilities
"""
family = socket.AF_INET if ip_version == 4 else socket.AF_INET6
def proc(host, family, queue):
try:
queue.put(socket.getaddrinfo(host, 0, family))
except socket.gaierror as ex:
# TODO: Would be nice if we could isolate just the not
# found error.
queue.put([])
except socket.timeout:
# Don't care, we just want the queue to be empty if
# there's an error.
pass
queue = Queue.Queue()
thread = threading.Thread(target=proc, args=(host, family, queue))
thread.setDaemon(True)
thread.start()
try:
results = queue.get(True, timeout)
if len(results) == 0:
return None
family, socktype, proto, canonname, sockaddr = results[0]
except Queue.Empty:
return None
# NOTE: Don't make any attempt to kill the thread, as it will get
# Python all confused if it holds the GIL.
(ip) = sockaddr
return str(ip[0])
评论列表
文章目录