Diese Erweiterung bietet eine Möglichkeit, den Zugriff auf Ihre Flask -Anwendung auf der Grundlage des Hostnamens oder der IP -Adresse oder des IP -Adressbereichs (Network) der Incoming -Anforderung) einzuschränken.
Installieren Sie das Paket mit PIP:
pip install flask-allowed-hostsAllowedHosts -Klasse.@allowed_hosts.limit() Dekorateur (optional). 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 )WARNUNG : Dieser Ansatz kann in Kombination mit der klassenbasierten Verwendung ein unerwartetes Verhalten verursachen.
@limit_hosts Decorator. 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!" }), 200Weitere Beispiele finden Sie im Beispielverzeichnis.
app : Die Flask -Anwendungsinstanz (optional).allowed_hosts : Liste der zulässigen Hosts (optional, standardmäßig None , die allen Hosts erlaubt).on_denied : Funktion für verweigertes Zugriffsverhalten (optional).Die Erweiterung respektiert diese Konfigurationen:
ALLOWED_HOSTS : Liste der zulässigen Hosts in Flask -Konfiguration.ALLOWED_HOSTS_ON_DENIED : Funktion für verweigertes Zugriffsverhalten in der Flask -Konfiguration.Vorrang : Werte, die während der Initialisierungsüberschreibungskolbenkonfigurationswerte bereitgestellt werden.
Sie können den Debug -Modus aktivieren, indem Sie die Umgebungsvariable ALLOWED_HOSTS_DEBUG auf True einstellen:
export ALLOWED_HOSTS_DEBUG= " True "Dadurch werden hilfreiche Debug -Nachrichten an die Konsole gedruckt.
Beiträge sind willkommen! Bitte zögern Sie nicht, eine Pull -Anfrage einzureichen.
Wenn Sie Fragen oder Feedback haben, können Sie eine Ausgabe oder eine Pull -Anfrage gerne öffnen.
Dieses Projekt ist unter der [MIT] -Lizenz lizenziert - Einzelheiten finden Sie in der lizenz.md -Datei.