【问题标题】:Bottle.py HTTP Auth?Bottle.py HTTP 身份验证?
【发布时间】:2012-10-27 16:11:41
【问题描述】:

如何让我的 bottle.py 应用程序(以 Paste 或 Cherrypy 运行)进行 HTTP(基本或摘要)身份验证? - 我需要保护它,但找不到任何 HOWTO。

【问题讨论】:

标签: python http authentication bottle digest


【解决方案1】:

bottle 有一个内置的 auth_basic 装饰器,可用于视图:

from bottle import auth_basic, request, route

def check(user, pw):
    # Check user/pw here and return True/False

@route('/')
@auth_basic(check)
def home():
    return { 'data': request.auth }

【讨论】:

  • 你能解释更多吗?我不确定如何检查。
  • 恐怕这不是很多信息。你的 check 函数完全取决于你想要做什么,所以它可以说例如if user == "user" and pw == "hello": return True——虽然一般来说我当然不会硬编码这样的密码!
  • 如果检查失败会怎样?你能决定显示什么吗?
  • auth_basic参数是要调用的函数,auth realm的名字,401错误中要显示的body,所以可以传入。
【解决方案2】:

GitHub 上有一些库,例如 https://github.com/FedericoCeratto/bottle-cork,应该会有所帮助。它可能比相关帖子中建议的 repoze 库更容易集成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    相关资源
    最近更新 更多