1.cookie可以用来保存一些简单的数据,他是通过设置header来实现的例如
'Set-Cookie: raspberrypi="Hello world"; \
expires=Wed, 28 Aug 2013 18:30:00 GMT\r\n\r\n'
在cookie和content一定要确保有一空行
2.
py提供了一个import Cookie模块以方便操作cookie
# 创建Cookie
c=Cookie.SimpleCookie()
# 写入值
c['raspberrypi']='Hello world'
# 设置过期时间
c['raspberrypi']['expires']=1*1*3*60*60
# 把header打印给client端
print c
通过Cookie模块我们创建了header
>>>
Set-Cookie: raspberrypi="Hello world"; expires=Thu, 01 Oct 2015 14:00:28 GMT
>>>
读取Cookie
if 'HTTP_COOKIE' in os.environ:
cookie_string=os.environ.get('HTTP_COOKIE')
c=Cookie.SimpleCookie()
c.load(cookie_string)
try:
data=c['raspberrypi'].value
print "cookie data: "+data+"<br>"
except KeyError:
print "The cookie was not set or has expired<br>"