title: Flask: How to get the Client IP Address of the Request?
body: <p><strong>Problem:</strong></p>
  <p>Hi all - I am basically trying to whitelist certain ip-addresses so that only users using a specific set of networks can access my Flask Microservice's REST endpoints.</p>
  <p><strong>Code Example:</strong></p>
  <p>I have a decorator like this:</p>
  <pre><code>def check_ip(f):
      @wraps(f)
      def decorated(*args, **kwargs):
          headers_list = request.headers.getlist(&quot;HTTP_X_FORWARDED_FOR&quot;)
          ip = headers_list[0] if headers_list else request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
          current_app.logger.info(
              {'client_ip_address': request.json['ip']
               }
          )
          if ip:
              if ip in allowed_ip_addresses:
                  return f(*args, **kwargs)
  
          return {'message': 'Forbidden to Access'}, 403
  
      return decorated
  </code></pre>
  <p><strong>Error:</strong></p>
  <p>The issue I'm facing is that the ip address detected is always 127.0.0.1 (localhost). I can't seem to get the correct IP address of the request. Any help would be greatly appreciated!</p>
  <p><strong>Desired Solution:</strong></p>
  <p>I would like to be able to acquire the correct ip address of the request sent to the API</p>
  <p><strong>Requirements:</strong></p>
  <p>I'm using Flask and when deployed to production, it's running on Gunicorn</p>