- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在我的应用中使用了 flask session。在我的一个处理程序中,我设置了 session 值,而在其他处理程序中没有设置 session 。但是我发现在每个响应中都有一个http header: Set Cookie
exists。为什么会这样?
app = Flask(__name__)
app.secret_key = r"A0Zr98j/3yX R~XHH!jmN'LWX/,?RT"
@app.route('/auth/login', methods=['POST'])
@crossdomain(origin='*')
def authlogin():
user = User(username=username, registered_at=sqlnow())
user.accounts = [Account(provider='weibo', access_token=access_token, uid=uid)]
account = user.accounts[0]
session['user_id'] = account.user_id
return jsonify({
'status': 'success',
'data': {
'user_id': account.user_id,
'uid': account.uid
}
})
@app.route('/api/movies/<movie_type>')
def moviescoming(movie_type):
if movie_type == 'coming':
return getmovies(MOVIE_TYPE_PLAYING, offset, limit)
else:
return getmovies(MOVIE_TYPE_COMING, offset, limit)
app.run(host='0.0.0.0', debug=True)
代码显示在这里: https://github.com/aisensiy/dianying/blob/master/index.py
最佳答案
这是设计使然,但最近 Flask 中的更改允许您通过使用 SESSION_REFRESH_EACH_REQUEST
选项更改此行为。截至发布此答案时,该选项尚未出现在 Flask 的稳定版本中。
让我们回过头来讨论一下 cookie 应该如何工作:
RFC 6265定义 a cookie should expire当代理(浏览器)声明 session 关闭时(通常是在浏览器关闭时),除非提供了某种机制来告诉浏览器 cookie 何时应该实际过期:
Unless the cookie's attributes indicate otherwise, the cookie [...]expires at the end of the current session (as defined by the useragent).
[...]
If a cookie has neither the Max-Age nor the Expires attribute, theuser agent will retain the cookie until "the current session is over"(as defined by the user agent).
如果服务器希望 cookie 在代理重新启动后继续存在,则需要设置过期时间。请注意,Expires 属性通常是首选,因为 Internet Explorer 有一个 history of poor support for max-age
。 .
因此,不可能说 cookie 应该是“永久的”。当人们谈论“永久”cookie 时,他们真正谈论的是在浏览器重新启动后仍然存在的 cookie。我知道有两种创建此“永久”cookie 的策略:
Set-Cookie
)以拥有它2 月 2 日到期。第一种方法要求 Set-Cookie
header 只设置给客户端一次(除非 cookie 内容需要更改)。
第二种方法需要在每次更新时发送 Set-Cookie
header ,以便在用户继续使用该服务时不断“推迟”过期。请注意,它也不是真正的“永久”,因为超过 31 天未使用您网站的用户的 cookie 将过期。
RFC 6265 在 defining the expiration date 上确实有一些话要说:
Although servers can set the expiration date for cookies to the
distant future, most user agents do not actually retain cookies for
multiple decades. Rather than choosing gratuitously long expiration
periods, servers SHOULD promote user privacy by selecting reasonable
cookie expiration periods based on the purpose of the cookie. For
example, a typical session identifier might reasonably be set to
expire in two weeks.
因此,虽然它没有明确说明是否要不断更新到期日期,但它似乎确实在说使用遥远的 future 日期不应被视为一种好的做法。
Flask 在设计上使用了第二种方法(使用 Set-Cookie
不断更新 cookie 过期时间)。默认情况下,cookie 的有效期为 future 31 天(可由 PERMANENT_SESSION_LIFETIME
配置)。对于每个请求,Flask 将使用另一个 Set-Cookie
将过期时间再推迟 31 天(或您将永久 session 生命周期值设置为的任何值)。因此,您看到的每个请求上的 Set-Cookie
都是预期的,即使 session 没有更改也是如此。
然而,最近在 pull request regarding using Set-Cookie
only when the cookie changes 中出现了讨论。 .这导致了 new feature允许用户更改其工作方式。 Flask 将继续按原样工作,但用户可以将新的 SESSION_REFRESH_EACH_REQUEST
选项设置为 False,这将导致 Set-Cookie
header 仅在 cookie 时发送变化。
新项目记录为:
this flag controls how permanent sessions are refresh [sic]. If set to
True
(which is the default) then the cookie is refreshed eachrequest which automatically bumps the lifetime. If set toFalse
aset-cookie
header is only sent if the session is modified. Nonpermanent sessions are not affected by this.
这个新选项与现有的 PERMANENT_SESSION_LIFETIME
一起,允许 Flask 开发人员更好地调整他们的“永久”cookie 将如何设置为过期。
截至此答案的发布日期(2013 年 12 月 24 日),SESSION_REFRESH_EACH_REQUEST
选项尚未成为任何 Flask 版本的一部分,因此希望使用它的用户需要等待 future 的 Flask发布。
关于python - flask 为每个响应设置 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20764566/
我正在遵循Flask-potion教程并尝试保护我的api。我一直试图找出如何在User端点上设置权限,以使一个User无法查看另一个User的记录。这是我的代码: class UserResourc
项目设置 Python 3.5.3 flask 0.12.2 目录 . ├── Core │ ├── BackgroundProcessManager.py │ ├── FirebaseDat
我正在构建 flask 应用程序并决定这次尝试应用程序工厂方法,但是遇到了 flask 迁移的麻烦并且无法找出简单的解决方案。 请注意,我想将配置位置作为选项传递给脚本 管理.py: manager
我尝试通过以下示例代码来熟悉Flask-APScheduler插件:https://github.com/viniciuschiele/flask-apscheduler/blob/master/ex
我一周前刚刚了解了 Flask 和 JWT,所以我真的需要一些帮助。有人可以解释一下flask-jwt和flask-jwt-extended之间的区别吗? 最佳答案 Flask-JWT 没有那么多功能
我是 Flask 的新手,我只是想用它创建一个简单的市场。我的网站上有三种类型的用户 [卖家、买家、管理员]。我认为每种类型的用户都应该有自己的注册和登录,因为他们的角色应该提供不同类型的信息。另一方
基本上,我想做一些非常简单的事情:我想创建一个用于删除数据库中条目的表单。 模板正在创建一个包含所有条目的html表,没有任何麻烦。我现在的问题是:如何将其转换为每行都有链接的表单。 当然,我可以通过
我对Flask来说非常陌生,其中一个基本要求是我需要SEO友好的URL。 我说一条路线 @app.route('/sales/') @app.route(/sales/) def get_sales(
一直在为用户管理系统开发Flask-user,但是我看不到自定义登录页面和注册页面的方法。我使用pip安装了flask-user pip install flask-user 任何帮助,将不胜感激 最
我有一个奇怪的情况,不知道如何找到问题。 我已经创建了一个这样的蓝图 reportjobsmod = Blueprint('jobreports', __name__, url_prefix='/jo
我认为在 Flask 中拥有实例变量的正确方法是添加用户和 session ,但我正在尝试测试一个概念,我还不想经历所有这些。我试图让一个网络应用程序将图像加载到一个变量中,然后可以对其执行不同的图像
select = SelectField("City") form.select.choices = [(places['name'], places['name']) for places in p
这个问题在这里已经有了答案: Provide extra information to Flask's app.logger (2 个回答) 3年前关闭。 我正在运行一个 flask 应用程序。我想写
我当时正在编程一个非常不错的api,使用json从游戏服务器到我的网站空间获取一些json数据, 但是每次我使用angular发送请求时,我都会得到以下信息:127.0.0.1--[20 / Mar
我已经开发了一个上传表单,以将特定的.xlsx文件作为上传。要求是要处理非xlsx上载的任何异常(例如zip,exe文件)。我正在使用pyexcel库读取上传内容。我尝试创建以下代码来处理此异常: e
我使用此功能在网站上捕获404错误: @app.errorhandler(404) def page_not_found(e): logger.warning('User raised an
我正在尝试同时使用 Flask Restful 和 Flask SocketIO。我已经制作了一个 Flask Restful 应用程序,但现在我想使用 Flask SocketIO 在客户端和我的服
我有一些 Flask 应用程序,我通过设置 FLASK_APP 来运行它们。环境变量和调用 flask run .自 FLASK_DEBUG也设置了,每当我更改一些代码时,应用程序都可以方便地重新加载
我目前正在使用 Flask 构建一个应用程序。我正在努力访问 Flask 实例“app”以及 Flask-mail 实例“mail”。 下面是我的项目的样子: └── my-project ├
Remember-me是 parameter flask_login.login_user(user, remember=False, duration=None, force=False, fres
我是一名优秀的程序员,十分优秀!