in tsqa/utils.py [0:0]
def poll_interfaces(hostports, **kwargs):
''' Block until we can successfully connect to all ports or timeout
:param hostports:
:param kwargs: optional timeout_sec
'''
connect_timeout_sec = 1
poll_sleep_sec = 0.1
if kwargs.has_key('timeout_sec'):
timeout = time.time() + kwargs['timeout_sec']
else:
timeout = time.time() + 5
hostports = hostports[:] # don't modify the caller's hostports
while timeout > time.time():
for hostport in hostports[:]: # don't modify our hostports copy during iteration
hostname = hostport[0]
port = hostport[1]
log.debug("Checking interface '%s:%d'", hostname, port)
# This supports IPv6
try:
s = socket.create_connection((hostname, port),
timeout=connect_timeout_sec,
)
s.close()
hostports.remove(hostport)
log.debug("Interface '%s:%d' is up", hostname, port)
except:
pass
if not hostports:
break
time.sleep(poll_sleep_sec)
if hostports:
raise Exception("Timeout waiting for interfaces: {0}".format(
reduce(lambda x, y: str(x) + ',' + str(y), hostports)))
log.debug("All interfaces are up")