def findLocalPort(ports):
 retVal = None
 for port in ports:
  try:
   try:
    s = socket._orig_socket(socket.AF_INET, socket.SOCK_STREAM)
   except AttributeError:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   s.connect((LOCALHOST, port))
   retVal = port
   break
  except socket.error:
   pass
  finally:
   try:
    s.close()
   except socket.error:
    pass
 return retVal

def findLocalPort ( ports ) : retVal = None for port in ports : try : try : s = socket . _orig_socket ( socket . AF_INET , socket . SOCK_STREAM ) except AttributeError : s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) s . connect ( ( LOCALHOST , port ) ) retVal = port break except socket . error : pass finally : try : s . close ( ) except socket . error : pass return retVal
find the first opened localhost port from a given list of ports .