wsgi cookies - 无中间件

发布于 2024-11-29 10:24:17 字数 1207 浏览 1 评论 0原文

听起来很简单

def create_cookie():
        bag = string.ascii_uppercase + string.ascii_lowercase + string.digits
        cookie = Cookie.SimpleCookie()
        cookie['sessionid'] = ''.join(random.sample(bag,24))
        cookie['sessionid']['expires'] = 600
        return 'Set-Cookie: ', cookie.output().replace('Set-Cookie: ', '', 1)

cookie.output() 就是 Set-Cookie: sessionid=YmsrvCMFapXk6wAt4EVKz2uU;过期=Sun, 14-Aug-2011 21:48:19 GMT

    headers.append(('Content-type', 'text/html'))
    headers.append(('Content-Length', str(output_len)))
    headers.append(create_cookie)

这是我的回复 ('200 OK', [('Content-type', 'text/html'), ('Content-Length', '1204'), ('Set-Cookie', 'sessionid=YmsrvCMFapXk6wAt4EVKz2uU;expires=Sun, 14 -Aug-2011 21:48:19 GMT')], 'html stuff')

这是我得到的环境: HTTP_COOKIE: sessionid=YmsrvCMFapXk6wAt4EVKz2uU

当我单击页面上的另一个链接时,不再有 HTTP_COOKIE 使用 chrome 开发控制台我可以看到请求 cookie 和页面标题包含: Cookie:: sessionid=YmsrvCMFapXk6wAt4EVKz2uU

现在,这让我有点困扰。首先,为什么它有双 :: ?我尝试在 create_cookie 函数中使用“Set-Cookie”而不是“Set-Cookie:”。这样做我根本没有从环境中获得任何 HTTP_COOKIE 。

因此,在网上进行了大量搜索之后,每个人都在谈论中间件(请不要建议我使用中间件 - 我这样做是为了学习 wsgi)...我一无所获。

Sounds simple enough

def create_cookie():
        bag = string.ascii_uppercase + string.ascii_lowercase + string.digits
        cookie = Cookie.SimpleCookie()
        cookie['sessionid'] = ''.join(random.sample(bag,24))
        cookie['sessionid']['expires'] = 600
        return 'Set-Cookie: ', cookie.output().replace('Set-Cookie: ', '', 1)

cookie.output() is Set-Cookie: sessionid=YmsrvCMFapXk6wAt4EVKz2uU; expires=Sun, 14-Aug-2011 21:48:19 GMT

    headers.append(('Content-type', 'text/html'))
    headers.append(('Content-Length', str(output_len)))
    headers.append(create_cookie)

This is my response
('200 OK', [('Content-type', 'text/html'), ('Content-Length', '1204'), ('Set-Cookie', 'sessionid=YmsrvCMFapXk6wAt4EVKz2uU; expires=Sun, 14-Aug-2011 21:48:19 GMT')], 'html stuff')

This is what I get from envirion:
HTTP_COOKIE: sessionid=YmsrvCMFapXk6wAt4EVKz2uU

And when I click another link on my page, no more HTTP_COOKIE
Using the chrome dev console I can see the request cookie and the page header contains:
Cookie:: sessionid=YmsrvCMFapXk6wAt4EVKz2uU

Now, this bothers me a bit. First of all why does it have double :: ? I tried using 'Set-Cookie' instead of 'Set-Cookie: ' in the create_cookie function. Doing that I didn't get any HTTP_COOKIE at all from environ.

So after lots of searching in the web and everyone just talking middleware (don't suggest I use one please - I'm doing this to learn the wsgi) ... I've come up empty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燃情 2024-12-06 10:24:17

看不见的默认行为ftw...

经过一些密集的调试后,我注意到以下请求不包含HTTP_COOKIE,这使得浏览器端实际发送我可以在浏览器中找到的cookie确实是一个问题。

一些挖掘发现默认路径和域行为破坏了我的努力,/action/login(设置 cookie 的位置)和 /display/data(未发送 cookie 的位置)之间的差异通过设置路径来修复这种情况下为“/

” 。

Invisible default behavior ftw...

After some intensive debugging I noticed that the following request didn't include the HTTP_COOKIE making it conclusively a problem on the browsers side of actually sending the cookie that I could find in the browser otherwise.

Some digging around revealed that the default path and domain behavior was spoiling my efforts , the difference between /action/login (where the cookie was set) and /display/data (where the cookie wasn't sent was fixed by setting the path in this case to '/'.

"yay"

没有伤那来痛 2024-12-06 10:24:17

你可以尝试:

return [tuple(line.split(': ',1)) for line in cookie.output().split('\r\n')]

这也适用于 cookie 中的多个条目。
当然,你需要使用extend而不是append:

headers.extend(create_cookie())

you could try:

return [tuple(line.split(': ',1)) for line in cookie.output().split('\r\n')]

This also works for multiple entries in cookie.
Of course, you need to use extend instead of append:

headers.extend(create_cookie())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文