mirror of
https://github.com/laurentS/slowapi.git
synced 2026-03-13 09:10:20 +08:00
27 lines
823 B
Python
27 lines
823 B
Python
from datetime import datetime, timedelta
|
|
from email.utils import parsedate_tz
|
|
from typing import Optional
|
|
|
|
from starlette.requests import Request
|
|
|
|
|
|
def get_ipaddr(request: Request) -> str:
|
|
"""
|
|
:return: 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:
|
|
r = request.headers["X_FORWARDED_FOR"]
|
|
return r
|
|
else:
|
|
return request.client.host or "127.0.0.1"
|
|
|
|
|
|
def get_remote_address(request: Request) -> str:
|
|
"""
|
|
:return: the ip address for the current request (or 127.0.0.1 if none found)
|
|
"""
|
|
return request.client.host or "127.0.0.1"
|