Created
August 28, 2012 21:04
-
-
Save eskerda/3504339 to your computer and use it in GitHub Desktop.
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/env python | |
import logging | |
import random | |
import socket | |
import sys | |
import urllib | |
HOST = "localhost" | |
PORT = 3000 | |
N_INSTANCES = 4 | |
logger = logging.getLogger('port_guesser') | |
logger.addHandler(logging.StreamHandler(sys.stderr)) | |
logger.setLevel(logging.DEBUG) | |
def socket_exists(host, port): | |
logger.info('Checking whether %s:%s is reachable' % (host, port)) | |
try: | |
socket.create_connection([host, port]) | |
except socket.error: | |
return False | |
else: | |
return True | |
def port_is_chunker(host, port): | |
try: | |
handle = urllib.urlopen("http://%s:%s" % (host, port)) | |
head = handle.info() | |
return 'TwistedWeb' in head['server'] | |
except Exception: | |
return False | |
def get_baseport(host): | |
baseport = 1024 | |
while not socket_exists(HOST, baseport) or not port_is_chunker(HOST, baseport): | |
baseport = baseport + 1 | |
if baseport == PORT: | |
baseport = baseport + 1 | |
return baseport | |
def main(): | |
ports = [] | |
baseport = get_baseport(HOST) | |
ports.append(baseport) | |
for i in range(N_INSTANCES-1): | |
baseport+=1 | |
ports.append(baseport) | |
logger.info('Found chunk ports: %s' % ports) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment