需求:
1、python的http接口服务器
2、接收form提交过来的请求内容
import os
import logging
import time
from pydantic import BaseModel
from fastapi import FastAPI, File, Form, Depends
import uvicorn
currentTime = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
logFileName = './evaLogs/' + 'evaLog_' + str(currentTime) + '.txt'
logging.basicConfig(filename=logFileName, format="%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s",
level=logging.DEBUG)
app = FastAPI()
class FileItem(BaseModel):
# Form内是None表示为空,有值即为默认值,...表示必须填
name: str = Form(None)
dataBase: str = Form("default")
rawNum: int = Form(...)
@app.post('/dataSet')
async def queryDataSet(request_data: FileItem):
logging.info("dataSet connect success")
print("data is ", request_data.name)
print("database is ", request_data.database)
print("num is ", request_data.rawNum)
return {"code": 0,
"success": True,
"message": "success"
}
if __name__ == '__main__':
uvicorn.run(app=app,
host="0.0.0.0",
port=12369,
workers=1)