Rah*_*ani 1 python cookies bottle
python新手.我使用bottle.py作为Web服务器.
我有一组需要在不同路由上呈现的静态HTML文件.我正在使用static_file()函数.我还想为页面设置基于会话的cookie.所以我使用response.set_cookie().
但事实证明,当我返回一个static_file时,cookie永远不会被设置.但是,如果我将响应更改为一个简单的字符串,set_cookie()工作正常.有谁能解释为什么?我该如何解决这个问题?
@app.route("/index")
def landingPage():
response.set_cookie("bigUId", "uid12345")
# return "Hello there"
return static_file("/html/index.html", root=config.path_configs['webapp_path'])
Run Code Online (Sandbox Code Playgroud)
欢迎使用Bottle和Python.:)
看看Bottle源代码,问题很明显.看看static_file
结局如何:
def static_file(...):
...
return HTTPResponse(body, **headers)
Run Code Online (Sandbox Code Playgroud)
static_file
创建一个新 HTTPResponse
对象 - 所以在此之前设置的任何标题都将被丢弃.
一个非常简单的方法是在你打电话后设置cookie static_file
,如下所示:
@app.route("/index")
def landingPage():
resp = static_file("/html/index.html", root=config.path_configs["webapp_path"])
resp.set_cookie("bigUId", "uid12345")
return resp
Run Code Online (Sandbox Code Playgroud)
我刚尝试过,它完美无缺.祝好运!
归档时间: |
|
查看次数: |
1191 次 |
最近记录: |