TypeError: 使用CherryPy 3.2基本认证时'уа'对象不可调用
我的网站是通过一个配置文件来设置CherryPy的。在这个配置文件里,我想要设置基本的身份验证。我已经指定了一个“checkpassword”函数的完整路径。但是我在tools.auth_basic.checkpassword这一行遇到了错误。
网上大多数示例都没有使用配置文件,这让事情变得更加复杂。
我的配置文件:
[/]
tools.auth_basic.on = True
tools.auth_basic.realm = "some site"
tools.auth_basic.checkpassword = "Infrastructure.App.Authentication.FindPassword"
我的startweb.py文件:
import ...
...
cherrypy.tree.mount(DesktopRootController(), "/", "auth.conf")
cherrypy.engine.start()
cherrypy.engine.block()
错误信息:
[10/Sep/2011:12:51:29] HTTP Traceback (most recent call last):
File "lib.zip\cherrypy\_cprequest.py", line 642, in respond
self.hooks.run('before_handler')
File "lib.zip\cherrypy\_cprequest.py", line 97, in run
hook()
File "lib.zip\cherrypy\_cprequest.py", line 57, in __call__
return self.callback(**self.kwargs)
File "lib.zip\cherrypy\lib\auth_basic.py", line 76, in basic_auth
if checkpassword(realm, username, password):
TypeError: 'str' object is not callable
我的“可调用”在这里定义:
import cherrypy
class Authentication:
def FindPassword(realm, username, password):
print realm
print username
print password
return "password"
这是“App”类的一部分:
from Authentication import Authentication
class App:
def __call__(self):
return self
def __init__(self):
self._authentication = Authentication
@property
def Authentication(self):
return _authentication
2 个回答
0
CherryPy的配置选项总是普通的Python值。如果你想描述一个模块变量,你需要找到一种方法把它导入到配置文件中。
[/]
tools.auth_basic.on = True
tools.auth_basic.realm = "some site"
tools.auth_basic.checkpassword = __import__("Infrastructure.App.Authentication").App.Authentication.FindPassword
补充:看起来CherryPy的选项解析器对import关键字有些问题;你需要使用一种更长、更不简洁的方式来处理。
补充2:接下来你遇到的问题是缺少self
参数。把你的认证类改成这样:
class Authentication:
def FindPassword(self, realm, username, password):
# ^^^^^
print realm
print username
print password
return "password"
0
解决办法来了!
首先,像这样修正配置文件。把函数名的引号去掉:
tools.auth_basic.checkpassword = Infrastructure.App.Authentication.FindPassword
其次,在checkpassword函数前面加上@staticmethod这个关键词:
@staticmethod
def FindPassword(realm, username, password):
print realm
print username
print password
return "password"