flask allowed hosts
1.0.0
ส่วนขยายนี้ให้วิธีการ จำกัด การเข้าถึงแอปพลิเคชัน Flask ของคุณตามชื่อโฮสต์หรือที่อยู่ IP หรือช่วงที่อยู่ IP (เครือข่าย)
ติดตั้งแพ็คเกจโดยใช้ PIP:
pip install flask-allowed-hostsAllowedHosts@allowed_hosts.limit() มัณฑนากร (ไม่บังคับ) 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 )คำเตือน : วิธีการนี้อาจทำให้เกิดพฤติกรรมที่ไม่คาดคิดเมื่อรวมกับการใช้งานในชั้นเรียน
@limit_hosts มัณฑนากร 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คุณสามารถค้นหาตัวอย่างเพิ่มเติมในไดเรกทอรีตัวอย่าง
app : อินสแตนซ์แอปพลิเคชัน Flask (ไม่บังคับ)allowed_hosts : รายการโฮสต์ที่อนุญาต (ไม่บังคับค่าเริ่มต้นถึง None ซึ่งอนุญาตให้โฮสต์ทั้งหมด)on_denied : ฟังก์ชั่นสำหรับพฤติกรรมการเข้าถึงที่ถูกปฏิเสธ (ไม่บังคับ)ส่วนขยายเกี่ยวข้องกับการกำหนดค่าเหล่านี้:
ALLOWED_HOSTS : รายการโฮสต์ที่อนุญาตใน Flask ConfigALLOWED_HOSTS_ON_DENIED : ฟังก์ชั่นสำหรับพฤติกรรมการเข้าถึงที่ถูกปฏิเสธใน Flask Configลำดับความสำคัญ : ค่าที่ให้ไว้ในระหว่างการกำหนดค่าเริ่มต้นค่าการกำหนดค่าขวดแทนที่
คุณสามารถเปิดใช้งานโหมดการดีบักได้โดยการตั้งค่าตัวแปรสภาพแวดล้อมที่อนุญาต ALLOWED_HOSTS_DEBUG เป็น True :
export ALLOWED_HOSTS_DEBUG= " True "สิ่งนี้จะพิมพ์ข้อความการดีบักที่เป็นประโยชน์ไปยังคอนโซล
ยินดีต้อนรับ! โปรดส่งคำขอดึง
หากคุณมีคำถามหรือข้อเสนอแนะใด ๆ โปรดเปิดปัญหาหรือคำขอดึง
โครงการนี้ได้รับใบอนุญาตภายใต้ใบอนุญาต [MIT] - ดูไฟล์ License.md สำหรับรายละเอียด