def select_random_ports(n):
 ports = []
 sockets = []
 for i in range(n):
  sock = socket.socket()
  sock.bind(('', 0))
  ports.append(sock.getsockname()[1])
  sockets.append(sock)
 for sock in sockets:
  sock.close()
 return ports

def select_random_ports ( n ) : ports = [ ] sockets = [ ] for i in range ( n ) : sock = socket . socket ( ) sock . bind ( ( '' , 0 ) ) ports . append ( sock . getsockname ( ) [ 1 ] ) sockets . append ( sock ) for sock in sockets : sock . close ( ) return ports
select and return n random ports that are available .