X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=bin%2FPortManager.py;h=a07bd40d49f1b72be4eb6b6a911875d47ab4828b;hb=a4874256219c549a9b1ff740d549391c4bf2f25f;hp=6ca830f8ba879a79bf57cde1d6998900d6302c14;hpb=920a4962b64d59019fcb8ef9f6a66d1ffb52b756;p=modules%2Fkernel.git diff --git a/bin/PortManager.py b/bin/PortManager.py index 6ca830f8b..a07bd40d4 100644 --- a/bin/PortManager.py +++ b/bin/PortManager.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: iso-8859-1 -*- -# Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE +# Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE # # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS @@ -23,11 +23,11 @@ # import os import sys +import psutil -try: - import cPickle as pickle #@UnusedImport -except: - import pickle #@Reimport +from socket import AF_INET, SOCK_STREAM + +import pickle __PORT_MIN_NUMBER = 2810 __PORT_MAX_NUMBER = 2910 @@ -103,41 +103,10 @@ def __isPortUsed(port, config): # def __isNetworkConnectionActiveOnPort(port): - # :NOTE: Under windows: - # netstat options -l and -t are unavailable - # grep command is unavailable - if sys.platform == "win32": - cmd = ['netstat','-a','-n','-p','tcp'] - else: - cmd = ['netstat','-ant'] - pass - - err = None - try: - from subprocess import Popen, PIPE, STDOUT - p = Popen(cmd, stdout=PIPE, stderr=STDOUT) - out, err = p.communicate() - except: - print("Error when trying to access active network connections.") - if err: print(err) - import traceback - traceback.print_exc() - return False - - from io import StringIO - buf = StringIO(out.decode('utf-8', 'ignore')) - ports = buf.readlines() - # search for TCP - LISTEN connections - import re - regObj = re.compile( ".*tcp.*:([0-9]+).*:.*listen", re.IGNORECASE ); - for item in ports: - try: - p = int(regObj.match(item).group(1)) - if p == port: return True - except: - pass - return False -# + # psutil realization + return port in [c.laddr.port for c in psutil.net_connections(kind='inet') if \ + (c.family, c.type, c.status) == (AF_INET, SOCK_STREAM, "LISTEN")] + # def getPort(preferredPort=None): logger.debug("GET PORT") @@ -154,7 +123,7 @@ def getPort(preferredPort=None): try: with open(config_file, 'rb') as f: config = pickle.load(f) - except: + except Exception: logger.debug("Problem loading PortManager file: %s"%config_file) # In this case config dictionary is reset