import uvicorn import socket def get_host_ip(): """ 查询本机ip地址 """ try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 80)) ip = s.getsockname()[0] except Exception: # 如果没有网络,回退到 host name ip = socket.gethostbyname(socket.gethostname()) finally: s.close() return ip if __name__ == "__main__": ip = get_host_ip() port = 18082 print(f" Server running on: http://{ip}:{port}") print(f" Docs available at: http://{ip}:{port}/docs") uvicorn.run("app.main:app", host="0.0.0.0", port=port)