def already_listening_socket(port, renewer=False):
 try:
  testsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
  testsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  try:
   testsocket.bind(('', port))
  except socket.error:
   display = zope.component.getUtility(interfaces.IDisplay)
   extra = ''
   if renewer:
    extra = RENEWER_EXTRA_MSG
   display.notification('Port {0} is already in use by another process. This will prevent us from binding to that port. Please stop the process that is populating the port in question and try again. {1}'.format(port, extra), force_interactive=True)
   return True
  finally:
   testsocket.close()
 except socket.error:
  pass
 return False

def already_listening_socket ( port , renewer = False ) : try : testsocket = socket . socket ( socket . AF_INET , socket . SOCK_STREAM , 0 ) testsocket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , 1 ) try : testsocket . bind ( ( '' , port ) ) except socket . error : display = zope . component . getUtility ( interfaces . IDisplay ) extra = '' if renewer : extra = RENEWER_EXTRA_MSG display . notification ( 'Port {0} is already in use by another process. This will prevent us from binding to that port. Please stop the process that is populating the port in question and try again. {1}' . format ( port , extra ) , force_interactive = True ) return True finally : testsocket . close ( ) except socket . error : pass return False
simple socket based check to find out if port is already in use .