增加flask启动端口检测,增加H2协议的支持。
This commit is contained in:
@@ -2,7 +2,11 @@ import html
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import signal
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import xml.etree.ElementTree as ET
|
||||
from pathlib import Path
|
||||
import cv2
|
||||
@@ -62,6 +66,56 @@ class AiUtils(object):
|
||||
# print(e)
|
||||
# return -1, -1
|
||||
|
||||
@classmethod
|
||||
def flask_port_free(cls,port):
|
||||
"""无需 psutil 的版本,通过系统命令查 PID"""
|
||||
|
||||
def can_bind(p):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
try:
|
||||
s.bind(("0.0.0.0", p))
|
||||
s.close()
|
||||
return True
|
||||
except OSError:
|
||||
s.close()
|
||||
return False
|
||||
|
||||
if can_bind(port):
|
||||
return
|
||||
|
||||
print(f"[ensure_port_free] Port {port} is occupied. Searching PID...")
|
||||
|
||||
pids = set()
|
||||
|
||||
if sys.platform.startswith("darwin") or sys.platform.startswith("linux"):
|
||||
cmd = f"lsof -t -i:{port}"
|
||||
proc = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
for line in proc.stdout.splitlines():
|
||||
if line.strip().isdigit():
|
||||
pids.add(int(line.strip()))
|
||||
|
||||
elif sys.platform.startswith("win"):
|
||||
cmd = f"netstat -ano | findstr :{port}"
|
||||
proc = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
for line in proc.stdout.splitlines():
|
||||
parts = line.split()
|
||||
if len(parts) >= 5 and parts[-1].isdigit():
|
||||
pids.add(int(parts[-1]))
|
||||
|
||||
else:
|
||||
raise RuntimeError("Unsupported platform for ensure_port_free")
|
||||
|
||||
for pid in pids:
|
||||
try:
|
||||
print(f"[ensure_port_free] Killing PID {pid}...")
|
||||
os.kill(pid, signal.SIGKILL)
|
||||
except Exception as e:
|
||||
print(f"[ensure_port_free] Failed to kill PID {pid}: {e}")
|
||||
|
||||
time.sleep(0.3)
|
||||
if not can_bind(port):
|
||||
raise RuntimeError(f"[ensure_port_free] Port {port} still occupied after kill.")
|
||||
|
||||
@classmethod
|
||||
def findImageInScreen(cls, target, udid):
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user