From 232df139f849d951a601a2028b8704c908cafe3d Mon Sep 17 00:00:00 2001 From: martsforever Date: Sat, 28 Mar 2026 21:49:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/utils/get_local_ips.py | 19 +++++++++++++++++++ main.py | 25 ++++++++++++++++++++++++- poetry.lock | 31 ++++++++++++++++++++++++++++++- pyproject.toml | 1 + 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 app/utils/get_local_ips.py diff --git a/app/utils/get_local_ips.py b/app/utils/get_local_ips.py new file mode 100644 index 0000000..c6cb90e --- /dev/null +++ b/app/utils/get_local_ips.py @@ -0,0 +1,19 @@ +import socket + +import psutil + + +def get_local_ips(): + ips = [] + try: + for interface, addrs in psutil.net_if_addrs().items(): + for addr in addrs: + if addr.family == socket.AF_INET and addr.address != '127.0.0.1': + ips.append(addr.address) + # 对获取到的 IP 地址列表进行排序 + ips.sort() + except Exception as e: + print(f"获取 IP 地址时出错:{e}") + return ips + +print(get_local_ips()) diff --git a/main.py b/main.py index 2eb42c4..f36f8bd 100644 --- a/main.py +++ b/main.py @@ -14,6 +14,7 @@ from pydantic import BaseModel, Field from starlette.staticfiles import StaticFiles from app.config.env import env +from app.utils.get_local_ips import get_local_ips app = FastAPI( docs_url=None, # 禁用默认 Swagger @@ -109,4 +110,26 @@ add_routes(app=app, runnable=model, path="/qwen") if __name__ == "__main__": import uvicorn - uvicorn.run(app, host="localhost", port=env.server_port) + # 获取环境变量中的端口号 + port = int(env.server_port) + + # 打印所有可用的访问地址 + print("\n服务已启动,以下是可用的访问地址:") + print(f" - 本地访问: http://127.0.0.1:{port}") + + for ip in get_local_ips(): + print(f" - 网络访问: http://{ip}:{port}") + + print("\n") # 空行美化输出 + + """ +uvicorn.run() 启动了一个异步事件循环来处理 HTTP 请求, +这个循环会一直运行直到服务器被手动停止(比如按下 Ctrl+C)。 +因此,uvicorn.run() 之后的代码不会被执行,直到服务器关闭。 + """ + # 启动Uvicorn服务器 + uvicorn.run("main:app", host="0.0.0.0", port=port) + + # uvicorn.run之后的代码永远都不会执行 + # 使用FastAPI的 @app.on_event("startup")装饰器可以在服务器成功启动后执行代码 + print('App is running...(Never Callable)') diff --git a/poetry.lock b/poetry.lock index ac1781e..8eb4d5a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1134,6 +1134,35 @@ type = "legacy" url = "https://pypi.tuna.tsinghua.edu.cn/simple" reference = "tsinghua" +[[package]] +name = "psutil" +version = "7.0.0" +description = "Cross-platform lib for process and system monitoring in Python. NOTE: the syntax of this script MUST be kept compatible with Python 2.7." +optional = false +python-versions = ">=3.6" +groups = ["main"] +files = [ + {file = "psutil-7.0.0-cp36-abi3-macosx_10_9_x86_64.whl", hash = "sha256:101d71dc322e3cffd7cea0650b09b3d08b8e7c4109dd6809fe452dfd00e58b25"}, + {file = "psutil-7.0.0-cp36-abi3-macosx_11_0_arm64.whl", hash = "sha256:39db632f6bb862eeccf56660871433e111b6ea58f2caea825571951d4b6aa3da"}, + {file = "psutil-7.0.0-cp36-abi3-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1fcee592b4c6f146991ca55919ea3d1f8926497a713ed7faaf8225e174581e91"}, + {file = "psutil-7.0.0-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4b1388a4f6875d7e2aff5c4ca1cc16c545ed41dd8bb596cefea80111db353a34"}, + {file = "psutil-7.0.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a5f098451abc2828f7dc6b58d44b532b22f2088f4999a937557b603ce72b1993"}, + {file = "psutil-7.0.0-cp36-cp36m-win32.whl", hash = "sha256:84df4eb63e16849689f76b1ffcb36db7b8de703d1bc1fe41773db487621b6c17"}, + {file = "psutil-7.0.0-cp36-cp36m-win_amd64.whl", hash = "sha256:1e744154a6580bc968a0195fd25e80432d3afec619daf145b9e5ba16cc1d688e"}, + {file = "psutil-7.0.0-cp37-abi3-win32.whl", hash = "sha256:ba3fcef7523064a6c9da440fc4d6bd07da93ac726b5733c29027d7dc95b39d99"}, + {file = "psutil-7.0.0-cp37-abi3-win_amd64.whl", hash = "sha256:4cf3d4eb1aa9b348dec30105c55cd9b7d4629285735a102beb4441e38db90553"}, + {file = "psutil-7.0.0.tar.gz", hash = "sha256:7be9c3eba38beccb6495ea33afd982a44074b78f28c434a1f51cc07fd315c456"}, +] + +[package.extras] +dev = ["abi3audit", "black (==24.10.0)", "check-manifest", "coverage", "packaging", "pylint", "pyperf", "pypinfo", "pytest", "pytest-cov", "pytest-xdist", "requests", "rstcheck", "ruff", "setuptools", "sphinx", "sphinx-rtd-theme", "toml-sort", "twine", "virtualenv", "vulture", "wheel"] +test = ["pytest", "pytest-xdist", "setuptools"] + +[package.source] +type = "legacy" +url = "https://pypi.tuna.tsinghua.edu.cn/simple" +reference = "tsinghua" + [[package]] name = "pydantic" version = "2.10.6" @@ -2297,4 +2326,4 @@ reference = "tsinghua" [metadata] lock-version = "2.1" python-versions = "^3.11" -content-hash = "cbd4a418461762ae481010557464342047763af195aba7f234f0e1e5b670bc27" +content-hash = "21dbb8586af0f40f50ddf8a878a5208b32a8cce259688672a4ca9bf71b728419" diff --git a/pyproject.toml b/pyproject.toml index 5943cae..603560b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ pydantic = "2.10.6" langgraph = "^1.1.3" langchain = {extras = ["openai"], version = "^1.2.13"} pydantic-settings = "2.10.1" +psutil = "7.0.0" [tool.poetry.group.dev.dependencies]