我收到错误AttributeError: 'SecureCookieSession' object has no attribute 'Session'
。我想创建基于令牌的身份验证。我通过一些服务获得令牌,这不是我关注的问题,但问题是我遇到了属性错误。
from flask import session, abort
from functools import wraps
app =Flask(__name__)
def authenticated_resource(function):
@wraps(function)
def decorated(*args, **kwargs):
if session.get("authenticated"):
return function(*args, **kwargs)
return abort(403) # unauthenticated
return decorated
@app.route('/login', methods=['GET', 'POST'])
def login():
error=None
if request.method=='POST':
if request.form['username']!='admin' or request.form['password']!='1234':
error ='Invalid Credentials. Please try again.'
else:
username=request.form['username']
password=request.form['password']
print 'username=', username
print 'password=', password
auth_url='http://192.168.206.133:5000/v2.0'
token = getToken(username=username, password=password, tenant_name=tenant_name)
session["authenticated"] = True
session["auth_token"] = token_dict
return redirect(url_for('getstats'))
return render_template('login.html', error=error)
@app.route('/metering')
@authenticated_resource
def getstats():
token =session.get("auth_token")
print 'token=',token
return render_template('metering.html')
@app.route('/projects')
def get_projects():
auth=v2.Password(auth_url="http://192.168.206.133:5000/v2.0/", username="admin", password="1234", tenant_id='020377f522324546b072af92862e6466')
sess=session.Session(auth=auth,verify=False)
nova = client.Client(2, session=sess)
instances =nova.servers.list()
def serializer(m):
ret={}
ret['Name']=m.name
ret['Id']= m.id
return ret
context ={
'instances': [serializer(m) for m in instances],
'project':[{'Id':'project', 'Name':'project'}] # Here added project option for group_by select option,
#previously It was showing only instance option, but now it is also show project option
}
return jsonify(context)
if __name__ == '__main__':
app.secret_key='super secret key'
#app.config['SESSION_TYPE']='filesystem'
app.debug= True
app.run(host='0.0.0.0',port=5001)
跟踪完整错误
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/vagrant/flaskhorizon/flaskhorizon/flaskhorizon.py", line 140, in get_projects
sess=session.Session(auth=auth,verify=False)
File "/usr/lib/python2.7/site-packages/werkzeug/local.py", line 338, in __getattr__
return getattr(self._get_current_object(), name)
AttributeError: 'SecureCookieSession' object has no attribute 'Session'
答案 0 :(得分:2)
您已导入Flask的session
对象。您似乎也期望session
引用包含Session
类的其他内容。你需要修复你的导入,这两者是截然不同的。您可以为其中一个导入添加别名,以使名称不重叠。或直接导入Session
课程。
from keystoneclient import session as ksc_session
ksc_session.Session(...)
# or
from keystoneclient.session import Session
Session(...)