【问题标题】:Flask - Blueprint - Before First Request?Flask - 蓝图 - 在第一次请求之前?
【发布时间】:2017-05-20 06:21:11
【问题描述】:

我正在尝试将before_first_request 功能添加到我的Flask 应用程序的特定Blueprint。下面你可以看到我有两个Blueprints,public 和 admin。

我试过这个但没有成功:https://stackoverflow.com/a/27401269/7077556

这仅适用于向应用程序发出的第一个请求,来自其他设备的任何其他请求和第一个请求之后的 ip 都不会由此“自定义”before_first_request 处理。

我想为客户向公众Blueprint 发出的第一个请求运行一个函数。

我该怎么做?提前致谢

这是我正在使用的代码:

from flask import Flask, Blueprint

application = Flask(__name__)

# PUBLIC Blueprint
public = Blueprint('public', __name__, static_url_path='/public', static_folder='static', template_folder='templates')

# ADMIN Blueprint
admin = Blueprint('admin', __name__, static_url_path='/admin', static_folder='static', template_folder='templates')


# Before First Request To Public BP
from threading import Lock
public._before_request_lock = Lock()
public._got_first_request = False

@public.before_request
def init_public_bp():
    if public._got_first_request:
        return
    else:
        with public._before_request_lock:
            public._got_first_request = True
            print('THIS IS THE FIRST REQUEST!')
            # Do more stuff here...


# PUBLIC ROUTE
@public.route("/")
def public_index():
    return 'Hello World!'

# ADMIN ROUTE
@admin.route('/')
def admin_index():
    return 'Admin Area!'

# Register PUBLIC Blueprint
application.register_blueprint(public)
# Register ADMIN Blueprint
application.register_blueprint(admin, url_prefix='/admin')

if __name__ == "__main__":
    application.run(host='0.0.0.0')

【问题讨论】:

    标签: python flask


    【解决方案1】:

    蓝图实例的before_request 不是装饰器。它只是一个实例方法,它需要在请求之前调用一个函数。您应该以这种方式使用它:

    from __future__ import print_function
    from flask import Flask, Blueprint
    
    application = Flask(__name__)
    
    # PUBLIC Blueprint
    public = Blueprint('public', __name__, static_url_path='/public', static_folder='static', template_folder='templates')
    
    # ADMIN Blueprint
    admin = Blueprint('admin', __name__, static_url_path='/admin', static_folder='static', template_folder='templates')
    
    
    # Before First Request To Public BP
    from threading import Lock
    public._before_request_lock = Lock()
    public._got_first_request = False
    
    def init_public_bp():
        if public._got_first_request:
            return  # or pass
        with public._before_request_lock:
            public._got_first_request = True
            print('THIS IS THE FIRST REQUEST!')
            # Do more stuff here...
    public.before_request(init_public_bp)
    
    
    # PUBLIC ROUTE
    @public.route("/")
    def public_index():
        return 'Hello World!'
    
    # ADMIN ROUTE
    @admin.route('/')
    def admin_index():
        return 'Admin Area!'
    
    # Register PUBLIC Blueprint
    application.register_blueprint(public)
    # Register ADMIN Blueprint
    application.register_blueprint(admin, url_prefix='/admin')
    
    if __name__ == "__main__":
        application.run(host='0.0.0.0')
    

    【讨论】:

    • 不幸的是,它不起作用。我从具有不同 IP 的不同设备发出了第一个请求和第二个请求。这是我的终端:(env) root@vps123:~/test$ python test.py * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) - - THIS IS THE FIRST REQUEST! 111.111.111.111 - - [19/May/2017 23:54:26] "GET / HTTP/1.1" 200 - 222.222.222.222 - - [19/May/2017 23:54:33] "GET / HTTP/1.1" 200 - 你可以看到第二个请求没有打印任何东西。
    • 答案在if public._got_first_request: 之后缺少一个块。它应该只想打印第一个请求,不是吗?
    • 是的,它应该只为第一个请求打印,而不仅仅是一次。如果用户 1 发出请求,它应该打印,那么用户 2 发出另一个请求,它应该再次打印,依此类推......因此,对于每个向网站发出第一个请求的新用户,它应该打印,以及来自同一用户的任何下一个请求不应该打印。也许与会话有关?我真的不知道
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多