我无法使用bottle.hook('before_request')
来完成这项工作,知道基本的身份验证弹出窗口为何从不显示吗?
from bottle import *
auth_enabled = True
@hook('before_request')
def custom_auth_basic():
def decorator(func):
def wrapper(*a, **ka):
if auth_enabled:
user, password = request.auth or (None, None)
if user is None or not check_credentials(user, password):
err = HTTPError(401, "Access denied")
err.add_header('WWW-Authenticate', 'Basic realm="Private"')
return err
return func(*a, **ka)
else:
return func(*a, **ka)
return wrapper
return decorator
def check_credentials(user, pw):
username = "test"
password = "test"
if pw == password and user == username:
return True
return False
@route('/')
def root():
return Response("Test")
run(host='localhost', port=8080)
我无法使其正常工作,并且说实话,我不完全理解这3个嵌套函数以及如何调用它们。它基于我之前遇到的一个问题:Enable or disable @auth_basic() programmatically
谢谢!