似乎无法在 Python 中访问 c​​ookie (2.4)

发布于 2024-10-08 11:51:45 字数 638 浏览 0 评论 0原文

我有一个 CGI 脚本,我已成功设置了一个 cookie(我可以在 Firefox/Chrome 中看到它!),其中包含(例如)名称 uid 和内容 1。我似乎不明白如何访问此 cookie来自另一个 CGI 脚本——而且我正在使用 Python 2.4,所以我发现的很多示例可能不适用。

此代码打印“can't get uid”,然后打印页面的其余部分:

c = Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE"))
print("Content-Type: text/html")
print c.output()
print("\n\n")
uid = c.get("uid")
#uid = c["uid"].value # this would create an error and page would fail totally
if uid is None: 
    print("can't get uid")
    uid = 1 # set manually to prevent the rest of the page from failing

我没有对 cookie 适用的域做任何可疑的事情,所以我不明白为什么这不能获取 uid 值。顺便说一句,如果我尝试打印 c.output(),它是空白的。

I have a CGI script for which I've successfully set a cookie (which I can see in Firefox/Chrome!) which has (say) the name uid and the content 1. I don't seem to understand how to access this cookie from another CGI script--and I'm working in Python 2.4 so a lot of the examples I've found may not apply.

This code prints "can't get uid" followed by the rest of the page:

c = Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE"))
print("Content-Type: text/html")
print c.output()
print("\n\n")
uid = c.get("uid")
#uid = c["uid"].value # this would create an error and page would fail totally
if uid is None: 
    print("can't get uid")
    uid = 1 # set manually to prevent the rest of the page from failing

I haven't done anything fishy with the domain the cookie applies to, so I don't understand why this doesn't grab the uid value. By the way, if I try to print c.output(), it's blank.

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

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

发布评论

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

评论(2

帥小哥 2024-10-15 11:51:45

首先,您确定网络服务器或框架正在设置 HTTP_COOKIE 环境变量吗?
否则,在您的脚本之一中,您可能希望将 cookie 存储在文件系统的 CookieJar 文件中,并从那里访问设置的 cookie。

import cookielib

COOKIEFILE = 'Cookies.lwp'
cookiejar = cookielib.LWPCookieJar()
cookiejar.load(COOKIEFILE)
cookiejar["uid"] = 1
cookiejar.save(COOKIEFILE)

加载相同的 cookiejar 并在另一个脚本中获取 uid。

First thing is are you sure the webserver or the framework is setting the HTTP_COOKIE environment variable?
Otherwise, in one of your script you may want to store the cookies in the CookieJar file in the file system and access the set cookies from there.

import cookielib

COOKIEFILE = 'Cookies.lwp'
cookiejar = cookielib.LWPCookieJar()
cookiejar.load(COOKIEFILE)
cookiejar["uid"] = 1
cookiejar.save(COOKIEFILE)

Load the same cookiejar and do the get of uid in the other script.

濫情▎り 2024-10-15 11:51:45

好吧,我想我已经明白了!我确认 os.environ.get("HTTP_COOKIE") 正在获取某些内容,然后在我的小测试中按照元素的顺序进行操作,直到它起作用。然后我在更复杂的脚本中重现了该顺序。 (具体来说:内容类型声明、两个换行符、获取 cookie、从 cookie 获取值,等等。)

我对 Python 和 CGI​​ 的主要了解是元素的顺序(从内容类型声明开始)非常挑剔。非常感谢您提供正确方向的提示。

Okay, I think I figured this out! I confirmed that os.environ.get("HTTP_COOKIE") was getting something, and then played with the order of the elements in my tiny test until it worked. Then I reproduced that order in my more complicated script. (Specifically: content type declaration, two newlines, get cookie, get value from cookie, everything else.)

The main thing I've learned about Python and CGI is that the order of elements (starting with the content type declaration) is very fussy. Thanks very much for the hints in the right direction.

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