def findFreePort(interface='127.0.0.1', type=socket.SOCK_STREAM):
 family = socket.AF_INET
 probe = socket.socket(family, type)
 try:
  probe.bind((interface, 0))
  return probe.getsockname()
 finally:
  probe.close()

def findFreePort ( interface = '127.0.0.1' , type = socket . SOCK_STREAM ) : family = socket . AF_INET probe = socket . socket ( family , type ) try : probe . bind ( ( interface , 0 ) ) return probe . getsockname ( ) finally : probe . close ( )
ask the platform to allocate a free port on the specified interface .