Flask Allowed Hosts
This extension provides a way to restrict access to your Flask application based on the incoming request's hostname or IP address.
Features
- Restrict access by hostname/IP address.
- Per-route configuration options.
- Customize denied access behavior.
- Two usage options: class-based or decorator-based.
Installation
Install the package using pip:
pip install flask-allowed-hosts
Usage
Class-Based Usage
- Initialize the
AllowedHostsclass. - Define allowed hosts (optional).
- Define a function for denied access behavior (optional).
- Apply access control to routes using
@allowed_hosts.limit()decorator (optional).
Example:
from flask import Flask, jsonify, abort
from flask_allowed_hosts import AllowedHosts
app = Flask(__name__)
ALLOWED_HOSTS = ["93.184.215.14", "api.example.com"]
def custom_on_denied():
error = {"error": "Oops! Looks like you are not allowed to access this page!"}
return jsonify(error), 403
allowed_hosts = AllowedHosts(app, allowed_hosts=ALLOWED_HOSTS, on_denied=custom_on_denied)
# Allows all incoming requests
@app.route("/api/public", methods=["GET"])
def public_endpoint():
data = {"message": "This is public!"}
return jsonify(data), 200
# Only allows incoming requests from "93.184.215.14" and "api.example.com"
@app.route("/api/private", methods=["GET"])
@allowed_hosts.limit()
def private_endpoint():
data = {"message": "This is private!"}
return jsonify(data), 200
# We can override the allowed_hosts list and the on_denied function for each route
@app.route("/api/private/secret", methods=["GET"])
@allowed_hosts.limit(allowed_hosts=["127.0.0.1"], on_denied=lambda: abort(404))
def secret_private_endpoint():
data = {"message": "This is very private!"}
return jsonify(data), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
Decorator-Based Usage (Legacy)
Warning: This approach might cause unexpected behavior when combined with the class-based usage.
- Define allowed hosts (optional).
- Define a function for denied access behavior (optional).
- Apply access control to routes using
@limit_hostsdecorator.
Example:
from flask import Flask, jsonify
from flask_allowed_hosts import limit_hosts
app = Flask(__name__)
ALLOWED_HOSTS = ["93.184.215.14", "api.example.com"]
def custom_on_denied():
error = {"error": "Custom Denied Response"}
return jsonify(error), 403
# Allows all incoming requests
@app.route("/api/public", methods=["GET"])
def public_endpoint():
data = {"message": "This is public!"}
return jsonify(data), 200
# Only allows incoming requests from "93.184.215.14" and "api.example.com"
@app.route("/api/private", methods=["GET"])
@limit_hosts(allowed_hosts=ALLOWED_HOSTS, on_denied=custom_on_denied)
def private_endpoint():
return jsonify({"message": "This is private!"}), 200
More Examples
You can find more examples in the examples directory.
Configuration
Initialization Parameters
app: The Flask application instance (optional).allowed_hosts: List of allowed hosts (optional, defaults toNonewhich allows all hosts).on_denied: Function for denied access behavior (optional).
Flask Config and Environment Variables
Flask Configuration
The extension respects these configurations:
ALLOWED_HOSTS: List of allowed hosts in Flask config.ALLOWED_HOSTS_ON_DENIED: Function for denied access behavior in Flask config.
Precedence: Values provided during initialization override Flask config values.
Environment Variables
You can enable debug mode by setting the ALLOWED_HOSTS_DEBUG environment variable to True:
export ALLOWED_HOSTS_DEBUG="True"
This will print helpful debug messages to the console.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
If you have any questions or feedback, please feel free to open an issue or a pull request.
License
This project is licensed under the [MIT] License - see the LICENSE.md file for details.
Metadata
Release files for flask-allowed-hosts 1.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| flask_allowed_hosts-1.1.2.tar.gz | 5.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flask_allowed_hosts-1.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.7 kB
Release files / flask_allowed_hosts-1.1.2.tar.gz
| Download URL | flask_allowed_hosts-1.1.2.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
976e5b66526439523e4be1ed00adb664650b3fde5ebf1d8d01203d56221ac7b4
|
|
BLAKE2b-256 checksum How to use checksums |
d1e2a830d69a27d4de220eda472c883a262f43bcb0425f65488cb678f3ab5af6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|
Release files / flask_allowed_hosts-1.1.2-py3-none-any.whl
| Download URL | flask_allowed_hosts-1.1.2-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1b70e771e10fad925f4ff6431bec6f2ae4e37d26de01b212e2424971711768bc
|
|
BLAKE2b-256 checksum How to use checksums |
3a3ffd4747ae78294275fef66cf0d9b415362c340829b874592f4895339a1865
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|