Created
May 9, 2012 10:41
-
-
Save witsch/2643652 to your computer and use it in GitHub Desktop.
helper script to wait until some process is listening to a given TCP port
This file contains 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 | |
from socket import socket, gethostbyname, AF_INET, SOCK_STREAM | |
from sys import argv, exit | |
from time import sleep | |
def waitfor(*ports, **kw): | |
timeout = kw.get('timeout', 30) | |
host = kw.get('host', 'localhost') | |
ip = gethostbyname(host) | |
ports = set(*ports) | |
while ports and timeout > 0: | |
for port in list(ports): | |
s = socket(AF_INET, SOCK_STREAM) | |
if s.connect_ex((ip, port)) == 0: | |
ports.remove(port) | |
s.close() | |
if ports: | |
sleep(1) | |
timeout -= 1 | |
return not bool(ports) | |
if __name__ == '__main__': | |
exit(not waitfor(map(int, argv[1:]))) # False is 0 for `exit`! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment