在 Python 中检索 Cookies
pythonserver side programmingprogramming更新于 2025/5/3 16:12:17
检索所有设置的 cookies 非常容易。Cookies 存储在 CGI 环境变量 HTTP_COOKIE 中,它们将具有以下形式 −
key1 = value1;key2 = value2;key3 = value3....
示例
以下是如何检索 cookies 的示例。
#!/usr/bin/python
# 导入模块以进行 CGI 处理
from os import environ
import cgi, cgitb
if environ.has_key('HTTP_COOKIE'):
for cookie in map(strip, split(environ['HTTP_COOKIE'], ';')):
(key, value ) = split(cookie, '=');
if key == "UserID":
user_id = value
if key == "Password":
password = value
print "User ID = %s" % user_id
print "Password = %s" % password
输出
这将为上述脚本设置的 cookie 产生以下结果 −
User ID = XYZ Password = XYZ123
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

