python – 用于用户身份验证的CherryPy自定义工具
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 用于用户身份验证的CherryPy自定义工具,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3327字,纯文字阅读大概需要5分钟。
内容图文
![python – 用于用户身份验证的CherryPy自定义工具](/upload/InfoBanner/zyjiaocheng/799/2fd8a520b9844679888b9bbb92a1b82c.jpg)
我正在尝试在CherryPy控制器类中设置一种简单的方法来装饰方法,这样如果用户尚未进行身份验证,则会将用户重定向到登录页面.我打算做一个基本的Python装饰器,但an answer here建议我使用CherryPy自定义工具代替.所以我试图这样做,但我不能让它工作.这就是我所拥有的:
def authenticate():
user = cherrypy.session.get('user', None)
if not user:
raise cherrypy.HTTPRedirect('/?errMsg=Please%20log%20in%20first')
cherrypy.tools.authenticate = cherrypy.Tool('on_start_resource', authenticate)
/ home页面是一个应限制为经过身份验证的用户的页面,所以我有:
@cherrypy.expose
@cherrypy.tools.authenticate
def home(self, **kwargs):
tmpl = TemplateDir.get_template('home.mako')
return tmpl.render()
但是,当我尝试启动我的网站时出现此错误:
Traceback (most recent call last):
File ".\example.py", line 3, in <module>
from controller.main import Root
File "C:\...\controller\main.py", line 9, in <module>
class Root(BaseModule):
File "C:\...\controller\main.py", line 19, in Root
@cherrypy.tools.authenticate
File "C:\Python26\lib\site-packages\cherrypy\_cptools.py", line 119, in
__call__ % self._name)
TypeError: The 'authenticate' Tool does not accept positional arguments; you must
use keyword arguments.
编辑:好的,如果我将自定义工具的使用更改为括号,我会收到不同的错误.
@cherrypy.expose
@cherrypy.tools.authenticate() # Magic parentheses...
def home(self, **kwargs):
...
现在我得到:
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\cherrypy\_cprequest.py", line 625, in respond
self.hooks.run('on_start_resource')
File "C:\Python26\lib\site-packages\cherrypy\_cprequest.py", line 97, in run
hook()
File "C:\Python26\lib\site-packages\cherrypy\_cprequest.py", line 57, in __call__
return self.callback(**self.kwargs)
File ".\example.py", line 40, in authenticate
user = cherrypy.session.get('user', None)
AttributeError: 'module' object has no attribute 'session'
编辑:我打开了会话:
cherrypy.tools.sessions.storage_type = 'file'
cherrypy.tools.sessions.storage_path = r'%s\sessions' % curDir
cherrypy.tools.sessions.timeout = 60
cherrypy.tree.mount(Root(), "/", config={
'/static': {
'tools.staticdir.on':True,
'tools.staticdir.dir':r'%s\static' % curDir,
},
'/': {
'tools.sessions.on':True,
}
})
当我第一次使用Web方法上的自定义工具装饰器加载页面时,出现此错误:
AttributeError: ‘module’ object has no attribute ‘session’
然后,当我重新加载页面时,我收到此错误:
AttributeError: ‘_Serving’ object has no attribute ‘session’
编辑:即使在我的控制器类中尝试了这么多,我仍然得到’模块对象没有属性会话’错误:
class Root(BaseModule):
_cp_config = {'tools.sessions.on': True}
sess = cherrypy.session # Error here
...
解决方法:
我正在使用错误的钩子.更改:
cherrypy.tools.authenticate = cherrypy.Tool('on_start_resource', authenticate)
至:
cherrypy.tools.authenticate = cherrypy.Tool('before_handler', authenticate)
解决了这个问题.显然我的身份验证方法在会话开启之前被调用,因此无法访问cherrypy.session.我的控制器中不需要任何会话开启的东西;所有必要的是我的服务器启动脚本中的以下内容:
def authenticate():
...
cherrypy.tools.authenticate = cherrypy.Tool('before_handler', authenticate)
cherrypy.tree.mount(Root(), "/", config={
"/": {
'tools.sessions.on':True,
'tools.sessions.storage_type':'file',
'tools.sessions.storage_path':r'%s\sessions' % curDir,
'tools.sessions.timeout':60
}, ...
})
然后,在我的控制器上使用受限制的方法:
@cherrypy.expose
@cherrypy.tools.authenticate()
def home(self, **kwargs):
...
内容总结
以上是互联网集市为您收集整理的python – 用于用户身份验证的CherryPy自定义工具全部内容,希望文章能够帮你解决python – 用于用户身份验证的CherryPy自定义工具所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。