TypeError:使用CherryPy 3.2基本身份验证时无法调用"str"对象 [英] TypeError: 'str' object is not callable when using CherryPy 3.2 Basic Authentication

查看:64
本文介绍了TypeError:使用CherryPy 3.2基本身份验证时无法调用"str"对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的站点通过配置文件配置CherryPy.在配置文件中,我尝试设置基本身份验证.我已经指定了"checkpassword"函数的完全限定路径.但是我收到关于tools.auth_basic.checkpassword行的错误.

My site configures CherryPy by way of a configuration file. In the configuration file I am trying to setup basic authentication. I have specified the fully qualified path to a "checkpassword" function. But I'm getting an error about the tools.auth_basic.checkpassword line.

大多数在线示例,请勿使用配置文件.因此,这使事情变得更加困难.

Most of the samples online, do not use a config file. So it's making things more difficult.

我的配置文件:

[/]
tools.auth_basic.on = True
tools.auth_basic.realm = "some site"
tools.auth_basic.checkpassword = "Infrastructure.App.Authentication.FindPassword"

我的startweb.py文件:

My startweb.py file:

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

我的可通话"在这里定义:

My "callable" is defined here:

import cherrypy

class Authentication:
    def FindPassword(realm, username, password):
        print realm
        print username
        print password
        return "password"

这是一些"App"类:

And this is some of the "App" class:

from Authentication import Authentication

class App:
    def __call__(self):
        return self

    def __init__(self):
        self._authentication = Authentication

    @property
    def Authentication(self):
        return _authentication

推荐答案

解决方案!

首先,像这样修复配置文件.删除函数名称中的引号:

First, fix the config file like this. Remove the quotes from the function name:

tools.auth_basic.checkpassword =  Infrastructure.App.Authentication.FindPassword

第二,将@staticmethod关键字添加到checkpassword函数:

Second, add the @staticmethod keyword to the checkpassword function:

@staticmethod
def FindPassword(realm, username, password):
    print realm
    print username
    print password
    return "password"

这篇关于TypeError:使用CherryPy 3.2基本身份验证时无法调用"str"对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆