使路由仅在烧瓶的调试模式下可访问 [英] Make a route only accessible in debug mode with flask

查看:53
本文介绍了使路由仅在烧瓶的调试模式下可访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆只希望在调试模式下访问的路由.是否有装饰器或其他允许我执行此操作的东西,或者我必须完全注释/删除代码? 示例:

I have a bunch of routes that I only want accessible in debug mode. Is there a decorator or something that allows me to do this or do I have to comment/delete the code entirely? Example:

@debug_only
@app.route("/send_data/<data>", methods=["GET", "POST"])
def send_data(data):
    return jsonfy("{'data': data}")

推荐答案

Flask为此不提供任何内置装饰器.编写一个装饰器,该装饰器检查current_app.debug,如果它不在调试模式下,则返回404.

Flask does not provide any built-in decorator for this. Write a decorator that checks current_app.debug and returns a 404 if it's not in debug mode.

from functools import wraps
from flask import current_app, abort

def debug_only(f):
    @wraps(f)
    def wrapped(**kwargs):
        if not current_app.debug:
            abort(404)

        return f(**kwargs)

    return wrapped

@app.route("/debug")
@debug_only
def debug_info():
    ...

这篇关于使路由仅在烧瓶的调试模式下可访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆