Files
---------/midlleware.py
T
2026-09-19 14:42:25 +08:00

20 lines
541 B
Python

import time
from fastapi import FastAPI, Response
app = FastAPI()
@app.middleware('http')
async def middl(req,next):
print('获取客户端的请求地址',req.client.host)
if req.client.host not in ['192.168.1.26','192.168.1.40','192.168.1.51']:
r = await next(req)
return r
else:
return Response(content='不可访问')
@app.middleware('http')
async def middl1(req,next):
a=time.time()
r = await next(req)
b=time.time()
print(f'执行的时间是{b-a}')
return r