mod_python的参考文档说道
Here is one way to use this function:
c = Cookie.Cookie(’spam’, ’eggs’, expires=time.time()+300)
Cookie.add_cookie(req, c)
Here is another:
Cookie.add_cookie(req, ’spam’, ’eggs’, expires=time.time()+300)
代码运行起来没有问题,但是就是往客户端种不上Cookie,郁闷了整整半天时间后,在邮件组中看到了这样一贴:
-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
There's a module called Cookie which provides an Dictionary-like interface
to access the multiple cookies in a request. In addition, each cookie class
has an output method that returns an HTTP-header-friendly string
("Set-Cookie: fig=newton"). The doc strings in Cookie.py give some pretty
good code examples.
In this module, a 'raw' cookie is called a morsel, so the terminology could
be a little confusing.
One warning though. The Cookie module is only designed to handle a single
value for each cookie (i.e. "Set-Cookie: MyCookie=MyValue") . Thus, if you
use a cookie to store more than one value (i.e. "Set-Cookie:
MyCookie=key1=value=&key2=value2&key3=value3"), you'll need to wrap Cookie
with your own class to handle the 'internal' values (or just build/parse the
value string yourself each time).
Chris
-----Original Message-----
From: Bo Lorentsen [mailto:bl at netgroup.dk]
Sent: Thursday, July 26, 2001 10:54 AM
To: riesch at nieuw.nl
Cc: mod_python at modpython.org
Subject: Re: [mod_python] Cookies
riesch at nieuw.nl wrote:
> I'm currently making a web-site with mod-python and since I want to make
use of cookies, I was wondering how I can set a cookie.
> I've been looking through the documentation provided on the web-site, but
I'm not able to find something. So if anyone could give me pointer, or even
better comething that works, I'd be much obliged.
You find them in the header fields (headers_in / headers_out ) in the
Request class.
If you like to set a cookie you do something like this :
req.headers_out[ "Set-Cookie" ] = "my_session_id=42"
and the next time this user sends back a request back to you ( and if he
support it) it will contain it in your input headers, and you will be able
to fetch it like this :
raw_cookie = req.headers_in[ "Cookie" ]
Now you only need to parse the input cookie string (raw_cookie), that have
the syntax as descried in RFC-2109.
mod_python does not have any helper classes for either in- or outgoing
cookies, but the format is strait forward, even I could make one :-)
Hope this helped
Regards
-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
用req.headers_out["Set-Cookie"]方法一试,果然成功了,但是用Cookie.headers_in["Cookie"]却报错,说没有Cookie字段,汗。最后,不伦不类的将req.headers_out["Set-Cookie"]和req.get_cookies()相结合,才算把cookie搞定,感觉很不爽,希望遇到同样问题的人能够用这种方法暂时应付一下,也希望有熟悉mod_python的人告诉我正确的使用方法是什么。