简体   繁体   English

阅读烧瓶会话Cookie

[英]Read Flask Session Cookie

I currently have the following: 我目前有以下内容:

from flask import Flask, session,request

@app.route('/venue/login', methods=['GET', 'POST'])
def venue_login():
    token = generate_token()
    session['venue_id'] = token
    return json.dumps(...)

When I look at the response in Chrome , I can see that Set-Cookie:session=... has been set. 当我在Chrome中查看response时,可以看到已经Set-Cookie:session=...

I have 2 questions: 我有两个问题:

1) How do I read this cookie on the `server? 1)我如何在`服务器上读取此cookie

I have tried: 我努力了:

venue_id = request.cookies.get('venue_id')

but this doesn't seem to be picking it up. 但这似乎并没有解决。

2) With my code above, all my cookies will be set with the same name. 2)使用上面的代码,我所有的cookies都将设置为相同的名称。 After reading the cookie value, I would like to delete the corresponding entry in session . 读取cookie值后,我想delete session的相应条目。 How should I go about doing this? 我应该怎么做呢? Also if two requests come in one after the other, will the line: 另外,如果两个requests一个接一个地出现,则该行将:

session['venue_id'] = token

override the first entry with the second? 用第二个覆盖第一个条目? Or does every request start a new session ? 还是每个request开始一个新session

I am kind of confused with how this all should work. 我对这一切应该如何工作感到困惑。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Well. 好。 Cookies and sessions are a bit different. Cookie和会话有些不同。

If you want to use cookies and make venue_id = request.cookies.get('venue_id') work - You need to use set_cookie : 如果您想使用Cookie并让venue_id = request.cookies.get('venue_id')起作用-您需要使用set_cookie
set_cookie('venue_id', token)
In cookies case - you can solve general problems that cookies can solve (have a long lasting cookie for example) 在Cookie的情况下-您可以解决Cookie可以解决的一般问题(例如,具有持久性的Cookie)

If you want to use session (which is intended for session uniqueness and auth) you need to just use session and put the "username" or the unique ID of the venue in your case. 如果要使用会话(用于会话唯一性和身份验证),则只需使用会话,然后在您的情况下使用“用户名”或场所的唯一ID。

Which to use - It really depends what you are trying to achieve 使用哪个-实际上取决于您要实现的目标

Have a look at: 看一下:
http://flask.pocoo.org/docs/0.10/quickstart/#cookies http://flask.pocoo.org/docs/0.10/quickstart/#sessions http://flask.pocoo.org/docs/0.10/quickstart/#cookies http://flask.pocoo.org/docs/0.10/quickstart/#sessions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM