![](/img/trans.png)
[英]How to make an app.route() in Flask only functional if you are coming from a specific URL?
[英]In Flask, is there a way to hide a @app.route from everyone but the app itself. My database can be seen from a url in JSON format
所以我在 FLASK 服務器上工作,使用 sqlite3、json、axios 和 VUEJS。 我的 VueJS 前端使用 axios 調用我的 sqlite3 數據庫。 在燒瓶端,數據庫通過 @app.route('/api/getdatabase', methods=['GET']) 與 jsonify 一起發送。 如果我在Flask服務器運行時將該 url 插入瀏覽器,我可以從http://localhost:5000/api/getdatabase看到 JSON 格式的整個數據庫。
有沒有辦法停止從瀏覽器訪問該 url 路由,同時保持與 axios 的“GET”連接?
我附加了 Flask @app.route,以防萬一在該 Flask 路由上調用“GET”的 VueJs 方法。
@app.route('/api/getdatabase', methods=['GET'])
def database_get():
if request.method == 'GET':
print("/getdatabase record")
conn = sql.connect('test.sqlite3')
cursor = conn.cursor()
cursor.execute("SELECT * FROM DATABASE ORDER By id")
rows = cursor.fetchall()
return jsonify(rows)
getDatabase() {
const path = "http://192.168.1.71:5000/api/getdatabase";
axios
.get(path)
.then(response => {
// console.log(res.data)
this.database = response.data;
})
.catch(error => {
console.log("Failed to get database");
});
},
您可以使該端點需要身份驗證
from flask import make_response
@app.route('/api/getdatabase', methods=['GET'])
def database_get():
if not request.headers.get('secret') == secret:
return app.response_class(
status=401,
mimetype='application/json',
response=json.dumps({'message': 'Invalid or missing secret'})
)
也許你想要的是一個 websocket。 看到: https : //flask-socketio.readthedocs.io/en/latest/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.