【问题标题】:Set-cookie then redirect using Python and HTMLSet-cookie 然后使用 Python 和 HTML 重定向
【发布时间】:2014-06-20 19:00:03
【问题描述】:

我在 Python 中使用 Cookie 库。

我正在尝试设置 cookie,然后将用户重定向到另一个页面,例如“http://example.com”。

这就是我设置 cookie 的方式:

cookie = Cookie.SimpleCookie()
cookie["session"] = random.randint(0,1000000000)
cookie["session"]["domain"] = ".bla.net"
cookie["session"]["path"] = "/itzhaki/"
print "Content-type: text/plain"
print cookie.output()
logging.debug(' Cookie set with: '+cookie.output())

我尝试删除“Content-type: text/plain”标头,但这是它实际设置 cookie 的唯一方法。

我尝试使用的重定向:

print "Location: http://example.com"

但这只会给我一个 500 服务器错误。

有什么建议吗?

【问题讨论】:

    标签: python html cookies


    【解决方案1】:

    在标题后添加一个空行。

    ...
    print cookie.output()
    print # <------------
    

    【讨论】:

    • 感谢您的评论。但答案恰恰相反,哈哈:)只有当我在 cookie.output() 之后删除空行时,它才终于起作用了。
    猜你喜欢
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 2010-09-26
    • 2021-03-20
    • 2012-12-31
    • 2015-08-29
    • 1970-01-01
    • 2015-11-20
    相关资源
    最近更新 更多