CherryPy webapp的基本身份验证

2024-04-27 15:23:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我只会在第一个网页加载前请求一个基本的用户名和密码。我使用了CherryPy文档中的示例:http://cherrypy.readthedocs.org/en/latest/basics.html#authentication

这是我的具体代码wsgi.py公司名称:

import cherrypy
from cherrypy.lib import auth_basic
from myapp import myapp

USERS = {'jon': 'secret'}

def validate_password(username, password):
    if username in USERS and USERS[username] == password:
       return True
    return False

conf = {
    '/': {
       'tools.auth_basic.on': True,
       'tools.auth_basic.realm': 'localhost',
       'tools.auth_basic.checkpassword': validate_password
    }
}

if __name__ == '__main__':

    cherrypy.config.update({
        'server.socket_host': '127.0.0.1',
        'server.socket_port': 8080,
    })

    # Run the application using CherryPy's HTTP Web Server
    cherrypy.quickstart(myapp(), '/', conf)

上面的代码将给我一个浏览器用户/密码提示,但是当我单击“确定”到该提示时,我得到以下错误:

^{pr2}$

我不知道它从哪里得到第三个论点。有什么想法吗?谢谢!在


Tags: 代码fromimportauth密码ifbasicusername
1条回答
网友
1楼 · 发布于 2024-04-27 15:23:25

从cherrypy的文件中

   checkpassword: a callable which checks the authentication credentials.
        Its signature is checkpassword(realm, username, password). where
        username and password are the values obtained from the request's
        'authorization' header.  If authentication succeeds, checkpassword
        returns True, else it returns False.

因此,checkpassword的实现必须遵循相同的api,即:checkpassword(realm, username, password),您向我们展示的是缺少第一个参数realm。在

相关问题 更多 >