28 lines
842 B
Python
28 lines
842 B
Python
from starlette.requests import Request
|
|
|
|
|
|
def get_ipaddr(request: Request) -> str:
|
|
"""
|
|
Returns the ip address for the current request (or 127.0.0.1 if none found)
|
|
based on the X-Forwarded-For headers.
|
|
Note that a more robust method for determining IP address of the client is
|
|
provided by uvicorn's ProxyHeadersMiddleware.
|
|
"""
|
|
if "X_FORWARDED_FOR" in request.headers:
|
|
return request.headers["X_FORWARDED_FOR"]
|
|
else:
|
|
if not request.client or not request.client.host:
|
|
return "127.0.0.1"
|
|
|
|
return request.client.host
|
|
|
|
|
|
def get_remote_address(request: Request) -> str:
|
|
"""
|
|
Returns the ip address for the current request (or 127.0.0.1 if none found)
|
|
"""
|
|
if not request.client or not request.client.host:
|
|
return "127.0.0.1"
|
|
|
|
return request.client.host
|