如何在 Python CGI 编程中检索 cookies?
阅读更多:Python 教程
检索 Cookies
检索所有设置的 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
此脚本设置的 cookies 将会产生以下结果 −
User ID = XYZ
Password = XYZ123