From 14bda817486fc58c8500cd2a62eb4c7b7f9c5273 Mon Sep 17 00:00:00 2001 From: chenhq Date: Thu, 10 Aug 2023 17:19:45 +0800 Subject: [PATCH] fix #197 --- FAQ.md | 2 +- solox/debug.py | 40 +++++++++-------------- solox/templates/index.html | 65 +++++++++++++++++++------------------- solox/view/apis.py | 2 +- 4 files changed, 50 insertions(+), 59 deletions(-) diff --git a/FAQ.md b/FAQ.md index c50305c..a04c090 100644 --- a/FAQ.md +++ b/FAQ.md @@ -101,7 +101,7 @@ * Python API收集:record=True。 * Mac电脑录制视频,请检查Scrcpy是否安装成功,可以自行安装:brew install scrcpy。 -## 1️⃣9️⃣ Android哪些指标以来app的进程需要存活? +## 1️⃣9️⃣ Android哪些指标依赖app的进程需要存活? * Cpu、Memory、Network、FPS * 界面如果不选择进程就点击Start收集,那么默认使用的是这个包名的主进程。 diff --git a/solox/debug.py b/solox/debug.py index 98337aa..dfaec55 100644 --- a/solox/debug.py +++ b/solox/debug.py @@ -3,11 +3,10 @@ import subprocess import time import os -import platform -import re import webbrowser import requests import socket +import psutil import sys from view.apis import api from view.pages import page @@ -74,23 +73,14 @@ def ip() -> str: def listen(port): - if platform.system() != 'Windows': - os.system("lsof -i:%s| grep LISTEN| awk '{print $2}'|xargs kill -9" % port) - else: - port_cmd = 'netstat -ano | findstr {}'.format(port) - r = os.popen(port_cmd) - r_data_list = r.readlines() - if len(r_data_list) == 0: - return - else: - pid_list = [] - for line in r_data_list: - line = line.strip() - pid = re.findall(r'[1-9]\d*', line) - pid_list.append(pid[-1]) - pid_set = list(set(pid_list))[0] - pid_cmd = 'taskkill -PID {} -F'.format(pid_set) - os.system(pid_cmd) + net_connections = psutil.net_connections() + conn = [c for c in net_connections if c.status == "LISTEN" and c.laddr.port == port] + if conn: + pid = conn[0].pid + logger.warning('Port {port} is used by process {pid}'.format(port, pid)) + logger.info('you can start solox : python -m solox --host={ip} --port={port}') + return False + return True def status(host: str, port: int): r = requests.get('http://{}:{}'.format(host, port), timeout=2.0) @@ -114,12 +104,12 @@ def start(host: str, port: int): def main(host=ip(), port=50003): try: - listen(port=port) - pool = multiprocessing.Pool(processes=2) - pool.apply_async(start, (host, port)) - pool.apply_async(open_url, (host, port)) - pool.close() - pool.join() + if listen(port=port): + pool = multiprocessing.Pool(processes=2) + pool.apply_async(start, (host, port)) + pool.apply_async(open_url, (host, port)) + pool.close() + pool.join() except KeyboardInterrupt: logger.info('stop solox success') sys.exit() diff --git a/solox/templates/index.html b/solox/templates/index.html index bbe2332..76043c5 100644 --- a/solox/templates/index.html +++ b/solox/templates/index.html @@ -414,42 +414,43 @@ }); $('.select-app').change(function() { - $.ajax({ - url: Host + "/package/pids", - type: "GET", - async: true, - cache: false, - data:{ - platform:platform, - device:$('.select-device').val(), - pkgname:this.value - }, - beforeSend: function () { - if(platform == 'Android'){ - $('.select-pid').empty(); - } - if(lan == 'cn' ){ - SwalLoading('初始化','正在获取该APP上的所有进程, 请您稍等!'); - }else{ - SwalLoading('Process initialization!','Initializing Process, please wait a mmoment.'); - } - - }, - success: function (data) { - if(data['status'] != 1 ) { + if(platform == 'Android'){ + $.ajax({ + url: Host + "/package/pids", + type: "GET", + async: true, + cache: false, + data:{ + platform:platform, + device:$('.select-device').val(), + pkgname:this.value + }, + beforeSend: function () { + if(platform == 'Android'){ + $('.select-pid').empty(); + } if(lan == 'cn' ){ - SwalFire('warning', '警告','没有发现该app的进程,请检查app是否已经启动', 5000); + SwalLoading('初始化','正在获取该APP上的所有进程, 请您稍等!'); }else{ - SwalFire('warning', 'Warning', data['msg'], 5000); - } - }else{ - for(var i=0;i'+data['pids'][i]+'') + SwalLoading('Process initialization!','Initializing Process, please wait a mmoment.'); + } + }, + success: function (data) { + if(data['status'] != 1 ) { + if(lan == 'cn' ){ + SwalFire('warning', '警告','没有发现该app的进程,请检查app是否已经启动', 5000); + }else{ + SwalFire('warning', 'Warning', data['msg'], 5000); + } + }else{ + for(var i=0;i'+data['pids'][i]+'') + } + swal.close(); } - swal.close(); } - } - }); + }); + } }); function initializeAPM(){ diff --git a/solox/view/apis.py b/solox/view/apis.py index 2ff1fe5..3cee1d9 100644 --- a/solox/view/apis.py +++ b/solox/view/apis.py @@ -122,7 +122,7 @@ def getPackagePids(): result = {'status': 0, 'msg': 'No process found, please start the app first.'} except Exception as e: logger.exception(e) - result = {'status': 0, 'msg': 'No process foundd, please start the app first.'} + result = {'status': 0, 'msg': 'No process found, please start the app first.'} return result @api.route('/package/activity', methods=['post', 'get'])