我的网站通过配置文件配置CherryPy。在配置文件中,我尝试设置基本身份验证。我已经指定了“checkpassword”函数的完全限定路径。但我收到有关tools.auth_basic.checkpassword行的错误。
Most of the samples online,请勿使用配置文件。所以这让事情变得更加困难。
我的配置文件:
[/]
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
答案 0 :(得分: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
编辑:在import关键字上看起来像cherrypy的选项解析器chokes;你将不得不使用更长,甚至更少的DRY形式。
Edit2:您遇到的下一个问题是缺少self
参数。将您的身份验证类更改为:
class Authentication:
def FindPassword(self, realm, username, password):
# ^^^^^
print realm
print username
print password
return "password"
答案 1 :(得分:0)
解决方案!
首先,像这样修复配置文件。从函数名称中删除引号:
tools.auth_basic.checkpassword = Infrastructure.App.Authentication.FindPassword
其次,将@staticmethod关键字添加到checkpassword函数:
@staticmethod
def FindPassword(realm, username, password):
print realm
print username
print password
return "password"