-
First Check
Commit to Help
Example Codeimport typing
from starlette.types import ASGIApp
from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint, DispatchFunction
class AllowIps(BaseHTTPMiddleware):
def __init__(self, app: ASGIApp, ips: typing.Optional[list] = None, dispatch: typing.Optional[DispatchFunction] = None) -> None:
super().__init__(app, dispatch)
self.ips = ips
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
response = await call_next(request)
if request.client.host not in self.ips:
print("xxxxxxxxxxx")
return {"msg": "ip not allowed"}
return responseDescription
Operating SystemLinux Operating System Detailscentos 7 FastAPI Version0.79.0 Python Version3.9 Additional ContextFile "/home/gamecpp/.local/share/virtualenvs/mgsev-tkZT_j_i/lib/python3.9/site-packages/starlette/middleware/errors.py", line 162, in call |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
i solved it |
Beta Was this translation helpful? Give feedback.
-
|
IT would be great if you would share how you solved it, for people running into the same problem and looking for an answer like you did :) |
Beta Was this translation helpful? Give feedback.
-
just return Response object, and it would be solved. like this: |
Beta Was this translation helpful? Give feedback.
just return Response object, and it would be solved. like this:
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
if request.client.host not in self.ips:
return Response("your ip not allowed")
else:
return await call_next(request)