fastApi 用户登录接口

from fastapi import FastAPI
from pydantic import BaseModel

from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()
#跨域访问
origins = [
    "http://localhost.tiangolo.com",
    "https://localhost.tiangolo.com",
    "http://127.0.0.1",
    "http://localhost:8080",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


class Getjson(BaseModel):
	user:str
	pas:str




@app.get("/")
def read_root():
    return {"Hello": "nihao"}

@app.post("/post")
def read_item(data:Getjson):
	try:
		if data.user=="xinhai" and data.pas=='xinhai':
			return {"msg":"登录成功","user": data.user, "pass":data.pas}
		else:
			return {"msg": "登录失败"}
	except:
		return {"msg": "参数不合法"}
#图片展示
from starlette.responses import StreamingResponse 
@app.get("/app/{names}")
async def getdate(*, names: str):
    f=open("app/"+names,"rb")
    buffer = io.BytesIO(f.read())  # 创建一个BytesIO,因为之前说过,可以使用like-file objects(和文件具有相同接口的对象)作为参数.
    r = StreamingResponse(buffer)  # 创建响应体
    return r
if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=83)

layui 表格数据
def getsql():
    conn = pymysql.connect(
        host="127.0.0.1",
    user="root", password ="root",
    database ="gg",
    charset ="utf8")

    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()  #
    cursor.execute("select * from user")
    parameters = cursor.fetchall()
    jsonData = []

    for row in range(len(parameters)):
        data = {}
        for i in range(len(cursor.description)):
            data[cursor.description[i][0]] = parameters[row][i]
        jsonData.append(data)

    tt = {'code': 0, "msg": "","count": len(jsonData),'data': jsonData}

    return tt

评论

刷新

友情链接