2017-10-17 82 views
-1

我已經嘗試過在google上搜索並查看一些文檔和示例代碼,但是我沒有得到如何讀取要在if語句上測試的cookie的值。獲取cookie的值#Python

我正在使用名稱'sessionid'創建cookie,並且它的值是用戶電子郵件的散列。

sessionid = hashlib.md5(user.encode('utf')).hexdigest() 
#generates the session id for the cookie 

response = make_response(open('LoginPassed.xml').read()) 
response.set_cookie('sessionid', sessionid) 

我要檢查,如果cookie存在,如果我可以說,特定的電子郵件訪問了網站...

for name in request.cookies: 

    #I am trying to get the value of a sessionid cookie to compare if that 
    #hash matches with the hash value of the email sent 

     value = request.cookies.get('sessionid') 
     # value = request.cookies I also tried this 
     items = value.items 

     for a, b in items: 

     if b == hashlib.md5(_email.encode('utf')).hexdigest(): 

      response = make_response() 
      response.headers['NewSession'] = 'new' 
      return LoginPassed(name) 
     return LoginError() 

我使用Python 3倍

如果我能找到cookie的名稱,我如何讀取它的值以與我的if語句上的某些內容進行比較?

回答

0
value = request.cookies.get('sessionid') 
# The request.cookies.get(nome_do_cookie) returns the value of the cookie 

    if value == hashlib.md5(_email.encode('utf')).hexdigest(): 
     print(value) 
     print(hashlib.md5(_email.encode('utf')).hexdigest()) 

     response = make_response() 
     response.headers['NewSession'] = 'new' 
     return LoginPassed(name) 
    return LoginError() 

問題是我沒有注意到我在做錯誤的檢​​查。