mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-06-02 00:57:41 +08:00
feat(web): auto-switch port on conflict and open browser on startup
This commit is contained in:
@@ -535,8 +535,24 @@ class WebChannel(ChatChannel):
|
|||||||
with open(file_path, 'r', encoding='utf-8') as f:
|
with open(file_path, 'r', encoding='utf-8') as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
|
||||||
|
def _find_available_port(self, start_port, max_tries=10):
|
||||||
|
"""从 start_port 开始向上找第一个可用端口,最多尝试 max_tries 次。"""
|
||||||
|
import socket
|
||||||
|
for port in range(start_port, start_port + max_tries):
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
try:
|
||||||
|
s.bind(("0.0.0.0", port))
|
||||||
|
return port
|
||||||
|
except OSError:
|
||||||
|
continue
|
||||||
|
raise OSError(f"[WebChannel] 无法找到可用端口(尝试范围 {start_port}-{start_port + max_tries - 1})")
|
||||||
|
|
||||||
def startup(self):
|
def startup(self):
|
||||||
port = conf().get("web_port", 9899)
|
base_port = conf().get("web_port", 9899)
|
||||||
|
port = self._find_available_port(base_port)
|
||||||
|
if port != base_port:
|
||||||
|
logger.info(f"[WebChannel] 端口 {base_port} 已被占用,自动切换到端口 {port}")
|
||||||
|
|
||||||
# 打印可用渠道类型提示
|
# 打印可用渠道类型提示
|
||||||
logger.info(
|
logger.info(
|
||||||
@@ -554,6 +570,9 @@ class WebChannel(ChatChannel):
|
|||||||
logger.info(f"[WebChannel] 🌐 本地访问: http://localhost:{port}")
|
logger.info(f"[WebChannel] 🌐 本地访问: http://localhost:{port}")
|
||||||
logger.info(f"[WebChannel] 🌍 服务器访问: http://YOUR_IP:{port} (请将YOUR_IP替换为服务器IP)")
|
logger.info(f"[WebChannel] 🌍 服务器访问: http://YOUR_IP:{port} (请将YOUR_IP替换为服务器IP)")
|
||||||
|
|
||||||
|
import webbrowser
|
||||||
|
webbrowser.open(f"http://localhost:{port}")
|
||||||
|
|
||||||
# 确保静态文件目录存在
|
# 确保静态文件目录存在
|
||||||
static_dir = os.path.join(os.path.dirname(__file__), 'static')
|
static_dir = os.path.join(os.path.dirname(__file__), 'static')
|
||||||
if not os.path.exists(static_dir):
|
if not os.path.exists(static_dir):
|
||||||
|
|||||||
Reference in New Issue
Block a user