From 8ce00805c589212a6eb1da95b2f708603a9d0c96 Mon Sep 17 00:00:00 2001 From: ubuntu <ubuntu@ubuntu.com> Date: Mon, 3 Sep 2018 12:18:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=BB=BA=E8=AE=AE=E6=8D=A2?= =?UTF-8?q?=E6=88=90tcp=20ping:=20443=E5=9B=9E=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clients/client-linux.py | 47 +++++++++++++++++++++++---------------- clients/client-psutil.py | 48 +++++++++++++++++++++++----------------- 2 files changed, 56 insertions(+), 39 deletions(-) diff --git a/clients/client-linux.py b/clients/client-linux.py index cf46ff3..b4ad4b7 100755 --- a/clients/client-linux.py +++ b/clients/client-linux.py @@ -161,47 +161,56 @@ lostRate = { '189': 0.0, '10086': 0.0 } -def _ping_thread(host, mark): - output = os.popen('ping -O %s &' % host) - lostCount = 0 - allCount = 0 +def _ping_thread(host, mark, port): + lostPacket = 0 + allPacket = 0 startTime = time.time() - output.readline() + while True: - buffer = output.readline() - if len(buffer) == 0: - return - if 'ttl' not in buffer: - lostCount += 1 - allCount += 1 - if allCount > 100: - lostRate[mark] = float(lostCount) / allCount + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(1) + try: + s.connect((host, port)) + except: + lostPacket += 1 + finally: + allPacket += 1 + s.close() + + if allPacket > 100: + lostRate[mark] = float(lostPacket) / allPacket + endTime = time.time() - if endTime-startTime > 3600: - lostCount = 0 - allCount = 0 + if endTime - startTime > 3600: + lostPacket = 0 + allPacket = 0 startTime = endTime + time.sleep(1) + def get_packetLostRate(): t1 = threading.Thread( target=_ping_thread, kwargs={ 'host': 'cu.tz.cloudcpp.com', - 'mark': '10010' + 'mark': '10010', + 'port': 443 } ) t2 = threading.Thread( target=_ping_thread, kwargs={ 'host': 'ct.tz.cloudcpp.com', - 'mark': '189' + 'mark': '189', + 'port': 443 } ) t3 = threading.Thread( target=_ping_thread, kwargs={ 'host': 'cm.tz.cloudcpp.com', - 'mark': '10086' + 'mark': '10086', + 'port': 443 } ) t1.setDaemon(True) diff --git a/clients/client-psutil.py b/clients/client-psutil.py index 24de503..f054426 100755 --- a/clients/client-psutil.py +++ b/clients/client-psutil.py @@ -129,48 +129,56 @@ lostRate = { '189': 0.0, '10086': 0.0 } -def _ping_thread(host, mark): - output = os.popen('ping -O %s &' % host if 'linux' in sys.platform else 'ping %s -t &' % host) - lostCount = 0 - allCount = 0 +def _ping_thread(host, mark, port): + lostPacket = 0 + allPacket = 0 startTime = time.time() - output.readline() - output.readline() + while True: - buffer = output.readline() - if len(buffer) == 0: - return - if 'TTL' not in buffer.upper(): - lostCount += 1 - allCount += 1 - if allCount > 100: - lostRate[mark] = float(lostCount) / allCount + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(1) + try: + s.connect((host, port)) + except: + lostPacket += 1 + finally: + allPacket += 1 + s.close() + + if allPacket > 100: + lostRate[mark] = float(lostPacket) / allPacket + endTime = time.time() - if endTime-startTime > 3600: - lostCount = 0 - allCount = 0 + if endTime - startTime > 3600: + lostPacket = 0 + allPacket = 0 startTime = endTime + time.sleep(1) + def get_packetLostRate(): t1 = threading.Thread( target=_ping_thread, kwargs={ 'host': 'cu.tz.cloudcpp.com', - 'mark': '10010' + 'mark': '10010', + 'port': 443 } ) t2 = threading.Thread( target=_ping_thread, kwargs={ 'host': 'ct.tz.cloudcpp.com', - 'mark': '189' + 'mark': '189', + 'port': 443 } ) t3 = threading.Thread( target=_ping_thread, kwargs={ 'host': 'cm.tz.cloudcpp.com', - 'mark': '10086' + 'mark': '10086', + 'port': 443 } ) t1.setDaemon(True)