fen*_*645 5 python cherrypy basic-authentication
我正在尝试创建一个非常基本的cherrypy webapp,它将在第一个(也是唯一一个)页面加载之前要求用户输入用户名和密码。我在这里使用了 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)
Run Code Online (Sandbox Code Playgroud)
上面的代码会给我一个浏览器用户/密码提示,但是当我点击确定提示时,我收到以下错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 667, in respond
self.hooks.run('before_handler')
File "/usr/local/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 114, in run
raise exc
TypeError: validate_password() takes exactly 2 arguments (3 given)
Run Code Online (Sandbox Code Playgroud)
我不确定它认为它从哪里得到第三个论点。有什么想法吗?谢谢!
来自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.
Run Code Online (Sandbox Code Playgroud)
因此,您的 checkpassword 实现必须遵循相同的 api,即:checkpassword(realm, username, password),并且您向我们展示的内容缺少第一个参数 - 领域。