Files
ai0824_0002/midlleware.py
T
2026-09-19 15:29:46 +08:00

20 lines
673 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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