cherrypy - CherryPy 工具:如何正确注册自定义工具?
问题描述
我想创建一个简单的工具,但无法正确注册。一旦我将它添加到任何方法中,我都会收到错误:
AttributeError: 'Toolbox' object has no attribute 'authenticate'
我试过了
cherrypy.tools.authenticate = cherrypy.Tool('before_handler', authenticate)
和
@cherrypy.tools.register('before_handler')
def authenticate():
我可能遇到的问题是将函数放在错误的位置。我有一个启动服务器和所有应用程序的主文件:
#config stuff
if __name__ == '__main__':
cherrypy.engine.unsubscribe('graceful', cherrypy.log.reopen_files)
logging.config.dictConfig(LOG_CONF)
cherrypy.tree.mount(app1(), '/app1')
cherrypy.tree.mount(app2(), '/app2')
cherrypy.quickstart(app3)
该文件由 systemd 单元启动。
如果我把这个authenticate
功能放在配置区域,它就不起作用。如果我直接将它放在其中一个应用程序中并且只在该应用程序中使用它,它就不起作用。总是同样的错误。
那么我必须把它放在哪里才能完成这项工作呢?
解决方案
我陷入python定义顺序陷阱的另一个案例。
不起作用:
class MyApp(object):
#....
@cherrypy.tools.register('on_start_resource')
def authenticate():
#....
作品:
@cherrypy.tools.register('on_start_resource')
def authenticate():
#....
class MyApp(object):
推荐阅读
- python - 将列中的 nan 替换为两个值 python 之间的平均值
- javascript - 如何使用 moment.JS 检查 SQL 获取的 dateTime 对象的时间戳是否比另一个更旧?
- html - 在 CakePHP 中将“i”标签嵌入到“a”标签中
- drag-and-drop - Vuetify TreeView + 拖放
- javascript - 尝试在移动视图中隐藏导航栏
- c - 当我们将值重新分配给 char 指针时,内存会发生什么?
- swift - 在 MVVM-C 中使用 TabBarController,Tab Bar 的协调器
- flutter - 我无法在颤振中使用依赖项中的其他包
- python - 数据库连接在 docker-compose 文件中停止工作
- php - 将这种字符 (