检索所有设置的cookie非常容易。Cookies存储在CGI环境变量HTTP_COOKIE中,它们具有以下形式-
key1 = value1;key2 = value2;key3 = value3....
这是有关如何获取Cookie的示例。
#!/usr/bin/python
# Import modules for CGI handling
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