Files
ai0824_0002/midlleware.py
T

20 lines
673 B
Python
Raw Normal View History

2026-09-19 15:29:46 +08:00
from fastapi import Response,Request
from time import time
async def fun_ip(r,n):
print('获取客户端的来访ip:',r.client.host)
if r.client.host not in ['192.168.5.18','192.168.5.6']:
respons = await n(r)
return respons
return Response(status_code=403,content='Not have Permission!')
async def fun_time(r:Request,n):
start_time = time() # 记录开始的时间
respons = await n(r) # 接口执行
end_time = time() # 记录结束的时间
sss = end_time - start_time
print( f'接口响应耗时:{sss}s' )
respons.headers["Time"] = str(sss) # 添加到响应头里,必须是字符串
return respons