#NEWLINE# def select_random_ports( n ) : #NEWLINE# #INDENT# 'Selects and return n random ports that are available.' #NEWLINE# ports = [ ] #NEWLINE# for i in range ( n ) : #NEWLINE# #INDENT# sock = socket . socket ( ) #NEWLINE# sock . bind ( ( '' , 0 ) ) #NEWLINE# while ( sock . getsockname ( ) [ 1 ] in _random_ports ) : #NEWLINE# #INDENT# sock . close ( ) #NEWLINE# sock = socket . socket ( ) #NEWLINE# sock . bind ( ( '' , 0 ) ) #NEWLINE# #UNINDENT# ports . append ( sock ) #NEWLINE# #UNINDENT# for ( i , sock ) in enumerate ( ports ) : #NEWLINE# #INDENT# port = sock . getsockname ( ) [ 1 ] #NEWLINE# sock . close ( ) #NEWLINE# ports [ port ] = port #NEWLINE# _random_ports . add ( port ) #NEWLINE# #UNINDENT# return ports