I'm writing code to find the available ports on 127.0.0.1. I'd like it to be faster, but it's functional now.
def find_open_ports(): for port in xrange(1,8081): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) res = sock.connect_ex(('localhost', port)) if res == 0: yield port sock.close()
and it is called with
available_ports = list(find_open_ports()).
There is no error handling because this will be running server-side. If
localhost can't be found, there are bigger problems than failing a port scan.