2015-08-25 62 views
-1

我收到錯誤AttributeError: 'SecureCookieSession' object has no attribute 'Session'。我想創建基於令牌的認證。我通過一些服務器獲得令牌,這不是我關心的問題,但問題是我得到屬性錯誤。AttributeError:'SecureCookieSession'對象沒有'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

什麼是錯誤的*完整回溯* –

+0

@MartijnPieters,感謝您的關注,我更新了錯誤的完整跟蹤...... – geeks

+0

'flaskhorizo​​n'您的項目?你可以向我們展示'get_projects()'代碼嗎?這是*代碼*在這裏失敗。 –

回答

1

已導入瓶的session對象。您似乎也期望session指的是其他類別包含Session類。您需要修復您的導入,以便兩者不同。您可以爲其中一個導入添加別名,以便名稱不重疊。或直接導入Session類。

from keystoneclient import session as ksc_session 
ksc_session.Session(...) 

# or 

from keystoneclient.session import Session 
Session(...)