Search code examples
python-3.xhttprequestipsanic

Sanic: difference between request.ip and request.remote_addr?


What is the difference between ip an remote_addr attributes of Sanic request?

Source code of Sanic framework:

@property
def ip(self):
    """
    :return: peer ip of the socket
    """
    if not hasattr(self, "_socket"):
        self._get_address()
    return self._ip


@property
def remote_addr(self):
    """Attempt to return the original client ip based on `forwarded`,
    `x-forwarded-for` or `x-real-ip`. If HTTP headers are unavailable or
    untrusted, returns an empty string.

    :return: original client ip.
    """
    if not hasattr(self, "_remote_addr"):
        self._remote_addr = self.forwarded.get("for", "")
    return self._remote_addr

Solution

  • Some proxies or load balancers could hide original client ip. Mentioned headers could store this value.

    For example, see documentation for X-Forwarded-For header