From 821d243b04c77aa14818ce567a4c68e310b9643d Mon Sep 17 00:00:00 2001 From: chenhq Date: Wed, 9 Aug 2023 15:02:22 +0800 Subject: [PATCH] fix recorded error in python api --- solox/__init__.py | 2 +- solox/debug.py | 118 +++++++++----------------------------- solox/public/apm.py | 2 +- solox/templates/base.html | 2 +- solox/web.py | 44 +++++++------- 5 files changed, 52 insertions(+), 116 deletions(-) diff --git a/solox/__init__.py b/solox/__init__.py index 70fc1ca..9f7ca32 100644 --- a/solox/__init__.py +++ b/solox/__init__.py @@ -2,4 +2,4 @@ from __future__ import absolute_import -__version__ = '2.7.0' +__version__ = '2.7.1' diff --git a/solox/debug.py b/solox/debug.py index 543b221..2270f00 100644 --- a/solox/debug.py +++ b/solox/debug.py @@ -11,15 +11,14 @@ import sys from view.apis import api from view.pages import page -from public.adb import adb from logzero import logger from threading import Lock from flask_socketio import SocketIO, disconnect from flask import Flask -import fire as fire -import signal import psutil - +import signal +from pyfiglet import Figlet +from solox import __version__ app = Flask(__name__, template_folder='templates', static_folder='static') app.register_blueprint(api) @@ -53,7 +52,6 @@ def backgroundThread(): logPath = os.path.join(os.getcwd(),'adblog',f'{current_time}.log') logcat = subprocess.Popen(f'adb logcat *:E > {logPath}', stdout=subprocess.PIPE, shell=True) - with open(logPath, "r") as f: while thread: socketio.sleep(1) @@ -72,39 +70,19 @@ def disconnect(): thread = False disconnect() - -def checkPyVer(): - """ - :func: check python version - """ - if int(platform.python_version().split('.')[0]) < 3: - logger.error('python version must be >2,your python version is {}'.format(platform.python_version())) - logger.error('please install python::3 and pip3 install -U solox') - sys.exit() - - -def _hostIP(): - """ - :func: get local ip - :return: ip - """ +def hostIP(): try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 80)) ip = s.getsockname()[0] except Exception as e: - raise e + ip = '127.0.0.1' finally: s.close() return ip def listeningPort(port): - """ - Detect whether the port is occupied and clean up - :param port: System port - :return: None - """ if platform.system() != 'Windows': os.system("lsof -i:%s| grep LISTEN| awk '{print $2}'|xargs kill -9" % port) else: @@ -123,84 +101,40 @@ def listeningPort(port): pid_cmd = 'taskkill -PID {} -F'.format(pid_set) os.system(pid_cmd) - def getServerStatus(host: str, port: int): - """ - get solox server status - :param host: - :param port: - :return: - """ - try: - r = requests.get(f'http://{host}:{port}', timeout=2.0) - flag = (True, False)[r.status_code == 200] - return flag - except requests.exceptions.ConnectionError: - pass - except Exception: - pass + r = requests.get('http://{}:{}'.format(host, port), timeout=2.0) + flag = (True, False)[r.status_code == 200] + return flag def openUrl(host: str, port: int): - """ - Listen and open the url after solox is started - :param host: - :param port: - :return: - """ flag = True while flag: - logger.info('Start solox server ...') + logger.info('start solox server ...') + f = Figlet(font="slant", width=300) + print(f.renderText("SOLOX {}".format(__version__))) flag = getServerStatus(host, port) - webbrowser.open(f'http://{host}:{port}/?platform=Android&lan=en', new=2) - logger.info(f'Running on http://{host}:{port}/?platform=Android&lan=en (Press CTRL+C to quit)') + webbrowser.open('http://{}:{}/?platform=Android&lan=en'.format(host, port), new=2) + logger.info('Running on http://{}:{}/?platform=Android&lan=en (Press CTRL+C to quit)'.format(host, port)) def startServer(host: str, port: int): - """ - startup the solox service - :param host: - :param port: - :return: - """ - try: - logger.info(f'Running on http://{host}:{port}/?platform=Android&lan=en (Press CTRL+C to quit)') - socketio.run(app, host=host, debug=False, port=port) - except Exception: - sys.exit(0) + socketio.run(app, host=host, debug=False, port=port) -def stopSolox(): - logger.info('stop python process') - pids = psutil.pids() - try: - for pid in pids: - p = psutil.Process(pid) - if p.name().__contains__('python'): - os.kill(pid, signal.SIGABRT) - except Exception as e: - logger.exception(e) - -def main(host=_hostIP(), port=50003): - """ - startup solox - :param host: 0.0.0.0 - :param port: 默认5000端口 - :return: - """ +def main(host=hostIP(), port=50003): try: - checkPyVer() listeningPort(port=port) - startServer(host, port) - # pool = multiprocessing.Pool(processes=2) - # pool.apply_async(startServer, (host, port)) - # pool.apply_async(openUrl, (host, port)) - # pool.close() - # pool.join() - except Exception: - sys.exit(0) + pool = multiprocessing.Pool(processes=2) + pool.apply_async(startServer, (host, port)) + pool.apply_async(openUrl, (host, port)) + pool.close() + pool.join() except KeyboardInterrupt: - stopSolox() - logger.info('stop solox success') + logger.info('stop solox success') + sys.exit() + except Exception as e: + logger.exception(e) + if __name__ == '__main__': - fire.Fire(main) \ No newline at end of file + main() \ No newline at end of file diff --git a/solox/public/apm.py b/solox/public/apm.py index dfde7ac..4e94f5f 100644 --- a/solox/public/apm.py +++ b/solox/public/apm.py @@ -517,7 +517,7 @@ def setPerfs(self): def collectAll(self): try: f.clear_file() - process_num = 6 if self.record else 7 + process_num = 7 if self.record else 6 pool = multiprocessing.Pool(processes=process_num) pool.apply_async(self.collectCpu) pool.apply_async(self.collectMemory) diff --git a/solox/templates/base.html b/solox/templates/base.html index c7d7dfc..1676431 100644 --- a/solox/templates/base.html +++ b/solox/templates/base.html @@ -130,7 +130,7 @@

  • - {% if lan == 'cn' %} 版本 {% else %} Releases {% endif %} . V2.7.0 + {% if lan == 'cn' %} 版本 {% else %} Releases {% endif %} . V2.7.1
  • diff --git a/solox/web.py b/solox/web.py index 84772a5..ed94fee 100644 --- a/solox/web.py +++ b/solox/web.py @@ -15,8 +15,6 @@ from threading import Lock from flask_socketio import SocketIO, disconnect from flask import Flask -import psutil -import signal from pyfiglet import Figlet from solox import __version__ @@ -45,9 +43,6 @@ def connect(): def backgroundThread(): global thread try: - # logger.info('Initializing adb environment ...') - # os.system('adb kill-server') - # os.system('adb start-server') current_time = time.strftime("%Y%m%d%H", time.localtime()) logPath = os.path.join(os.getcwd(),'adblog',f'{current_time}.log') logcat = subprocess.Popen(f'adb logcat *:E > {logPath}', stdout=subprocess.PIPE, @@ -83,16 +78,23 @@ def hostIP(): def listeningPort(port): - 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}') - # os.kill(pid, signal.SIGABRT) - return False - return True - + 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) def getServerStatus(host: str, port: int): r = requests.get('http://{}:{}'.format(host, port), timeout=2.0) @@ -116,12 +118,12 @@ def startServer(host: str, port: int): def main(host=hostIP(), port=50003): try: - if listeningPort(port=port): - pool = multiprocessing.Pool(processes=2) - pool.apply_async(startServer, (host, port)) - pool.apply_async(openUrl, (host, port)) - pool.close() - pool.join() + listeningPort(port=port) + pool = multiprocessing.Pool(processes=2) + pool.apply_async(startServer, (host, port)) + pool.apply_async(openUrl, (host, port)) + pool.close() + pool.join() except KeyboardInterrupt: logger.info('stop solox success') sys.exit()