Skip to main content

Flask extension to get real IP addresses behind proxies

Project description

Flask-RealIP

A Flask extension that obtains the real IP address of clients behind proxies.

Description

Flask-RealIP is a simple extension for Flask applications that automatically determines the real IP address of incoming requests, even when your application is behind one or more proxies. It properly handles:

  • X-Forwarded-For and other proxy headers
  • IPv4 and IPv6 addresses
  • IPv4-mapped IPv6 addresses
  • Address validation to prevent spoofing

Installation

You can install Flask-RealIP using pip:

pip install flask-realip

Or from the source code:

git clone https://github.com/tn3w/flask-realip.git
cd flask-realip
pip install -e .

Usage

Basic Usage

from flask import Flask, request
from flask_realip import RealIP

app = Flask(__name__)
real_ip = RealIP(app)

@app.route('/')
def index():
    return f"Your IP address is: {request.remote_addr}"

Configuration

Flask-RealIP can be configured with the following options:

# Initialize with custom options
real_ip = RealIP(
    app=app,
    trusted_proxies=['127.0.0.1', '10.0.0.0/8'],
    forwarded_headers=['HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR'],
    proxied_only=True
)

Or using Flask's configuration system:

# Configure in Flask app
app = Flask(__name__)
app.config['REAL_IP_TRUSTED_PROXIES'] = ['127.0.0.1', '10.0.0.0/8']
app.config['REAL_IP_FORWARDED_HEADERS'] = ['HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR']
app.config['REAL_IP_PROXIED_ONLY'] = True

real_ip = RealIP(app)

With Application Factory Pattern

from flask import Flask
from flask_realip import RealIP

real_ip = RealIP()

def create_app():
    app = Flask(__name__)
    # Configure Flask app...
    
    real_ip.init_app(app)
    return app

Configuration Options

  • trusted_proxies: List of trusted proxy IP addresses that are allowed to set forwarding headers. Default: ['127.0.0.1', '::1']
  • forwarded_headers: List of headers to check for forwarded IPs, in order of preference. Default: ['HTTP_X_FORWARDED_FOR', 'HTTP_X_REAL_IP', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED']
  • proxied_only: If True, only apply the middleware for requests from trusted proxies. Default: True

How It Works

When a request passes through proxies, the original client IP gets stored in headers like X-Forwarded-For. Flask-RealIP examines these headers from trusted proxies, validates the IP addresses, and makes them available through Flask's standard request.remote_addr.

License

Copyright 2025 TN3W

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_realip-1.1.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_realip-1.1.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file flask_realip-1.1.1.tar.gz.

File metadata

  • Download URL: flask_realip-1.1.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for flask_realip-1.1.1.tar.gz
Algorithm Hash digest
SHA256 55a75e04aa6795fb1d55b9f59f13d9b05e2f9454747d1ed727d73ebd13356078
MD5 1404f2062eb4430b650b9358345ae6e7
BLAKE2b-256 e5abd256d09682512dbf8f13e05e8323f3e3fb3afd9cdac120662507f7c2415f

See more details on using hashes here.

File details

Details for the file flask_realip-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: flask_realip-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for flask_realip-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7346cc212aaa9a0f256afaa5ba69969ea8c27e5c76532bc3c40c277711f4902e
MD5 853a34228f115feb7cff2b2f56a41d27
BLAKE2b-256 191f274d606cb6291dc2864c3eaa31dbdbb1c6b82c0e6b4104adc12c5c73bd75

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page