[英]TypeError: 'str' object is not callable when using CherryPy 3.2 Basic Authentication
My site configures CherryPy by way of a configuration file. 我的站点通过配置文件配置CherryPy。 In the configuration file I am trying to setup basic authentication.
在配置文件中,我尝试设置基本身份验证。 I have specified the fully qualified path to a "checkpassword" function.
我已经指定了“ checkpassword”函数的完全限定路径。 But I'm getting an error about the tools.auth_basic.checkpassword line.
但是我收到关于tools.auth_basic.checkpassword行的错误。
Most of the samples online , do not use a config file. 大部分在线示例 ,不使用配置文件。 So it's making things more difficult.
因此,这使事情变得更加困难。
My config file: 我的配置文件:
[/]
tools.auth_basic.on = True
tools.auth_basic.realm = "some site"
tools.auth_basic.checkpassword = "Infrastructure.App.Authentication.FindPassword"
My startweb.py file: 我的startweb.py文件:
import ...
...
cherrypy.tree.mount(DesktopRootController(), "/", "auth.conf")
cherrypy.engine.start()
cherrypy.engine.block()
The error message: 错误信息:
[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
My "callable" is defined here: 我的“ callable”在这里定义:
import cherrypy
class Authentication:
def FindPassword(realm, username, password):
print realm
print username
print password
return "password"
And this is some of the "App" class: 这是一些“ App”类:
from Authentication import Authentication
class App:
def __call__(self):
return self
def __init__(self):
self._authentication = Authentication
@property
def Authentication(self):
return _authentication
CherryPy config options are always regular python values. CherryPy配置选项始终是常规python值。 If you want to describe a module variable, you have to find a way to import it into the config file.
如果要描述模块变量,则必须找到一种将其导入配置文件的方法。
[/]
tools.auth_basic.on = True
tools.auth_basic.realm = "some site"
tools.auth_basic.checkpassword = __import__("Infrastructure.App.Authentication").App.Authentication.FindPassword
Edit: It looks like cherrypy's option parser chokes on the import keyword; 编辑:看起来cherrypy的选项解析器在import关键字上令人窒息; you'll have to use the even longer, even less DRY form like this.
您将不得不使用更长,甚至更少的DRY表单,就像这样。
Edit2: the next problem you have is a missing self
parameter. Edit2:下一个问题是缺少
self
参数。 Change your authentication class to this: 将身份验证类更改为此:
class Authentication:
def FindPassword(self, realm, username, password):
# ^^^^^
print realm
print username
print password
return "password"
The solution! 解决方案!
First, fix the config file like this. 首先,像这样修复配置文件。 Remove the quotes from the function name:
从函数名称中删除引号:
tools.auth_basic.checkpassword = Infrastructure.App.Authentication.FindPassword
Second, add the @staticmethod keyword to the checkpassword function: 其次,将@staticmethod关键字添加到checkpassword函数中:
@staticmethod
def FindPassword(realm, username, password):
print realm
print username
print password
return "password"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.