Agent开发中,如何写一个简单的http请求节点:fastapi、ngrok、mysql
·
FastAPI 数据库测试服务部署文档
概述
本文档介绍如何部署一个基于 FastAPI 的数据库测试服务,并通过 ngrok 将其暴露为公网可访问的 HTTPS 服务。
环境要求
- Python 3.7+
- FastAPI
- SQLAlchemy
- uvicorn
- ngrok
文件准备
创建 test.py 文件,内容如下:
from fastapi import FastAPI
from sqlalchemy import create_engine, text
# 数据库连接信息
DB_URL = "xxxxxxxxxxxxxxxx"
engine = create_engine(DB_URL)
app = FastAPI()
@app.get("/test_db")
def test_db():
try:
with engine.connect() as conn:
tables = conn.execute(text("SHOW TABLES")).fetchall()
table_list = [t[0] for t in tables]
return {
"status": "success",
"tables": table_list
}
except Exception as e:
return {
"status": "error",
"message": str(e)
}
部署步骤
1. 启动 FastAPI 服务
在当前文件夹下打开终端,执行以下命令启动服务:
uvicorn test:app --host 0.0.0.0 --port 8000
2. 配置 ngrok 隧道
打开一个ngork终端窗口,执行以下命令启动 ngrok:
ngrok http 8000
执行后,ngrok 会生成一个新的 HTTPS 地址,格式类似:https://xxx-xx-xxx-xxx-xx.ngrok-free.app
3. 测试 API
打开APIpost,对应url:https://xxxxxxxx/query_product
点击raw,设置对应JSON。
更多推荐


所有评论(0)