def already_listening_psutil(port, renewer=False):
 try:
  net_connections = psutil.net_connections()
 except psutil.AccessDenied as error:
  logger.info('Access denied when trying to list network connections: %s. Are you root?', error)
  return False
 listeners = [conn.pid for conn in net_connections if ((conn.status == 'LISTEN') and (conn.type == socket.SOCK_STREAM) and (conn.laddr[1] == port))]
 try:
  if (listeners and (listeners[0] is not None)):
   pid = listeners[0]
   name = psutil.Process(pid).name()
   display = zope.component.getUtility(interfaces.IDisplay)
   extra = ''
   if renewer:
    extra = RENEWER_EXTRA_MSG
   display.notification('The program {0} (process ID {1}) is already listening on TCP port {2}. This will prevent us from binding to that port. Please stop the {0} program temporarily and then try again.{3}'.format(name, pid, port, extra), force_interactive=True)
   return True
 except (psutil.NoSuchProcess, psutil.AccessDenied):
  pass
 return False

def already_listening_psutil ( port , renewer = False ) : try : net_connections = psutil . net_connections ( ) except psutil . AccessDenied as error : logger . info ( 'Access denied when trying to list network connections: %s. Are you root?' , error ) return False listeners = [ conn . pid for conn in net_connections if ( ( conn . status == 'LISTEN' ) and ( conn . type == socket . SOCK_STREAM ) and ( conn . laddr [ 1 ] == port ) ) ] try : if ( listeners and ( listeners [ 0 ] is not None ) ) : pid = listeners [ 0 ] name = psutil . Process ( pid ) . name ( ) display = zope . component . getUtility ( interfaces . IDisplay ) extra = '' if renewer : extra = RENEWER_EXTRA_MSG display . notification ( 'The program {0} (process ID {1}) is already listening on TCP port {2}. This will prevent us from binding to that port. Please stop the {0} program temporarily and then try again.{3}' . format ( name , pid , port , extra ) , force_interactive = True ) return True except ( psutil . NoSuchProcess , psutil . AccessDenied ) : pass return False
psutil variant of the open port check .