mod_python 不要搞我!!!

本文介绍了使用mod_python进行Cookie设置的方法。通过req.headers_out[Set-Cookie]成功设置Cookie,但req.get_cookies()方法存在问题。最终结合使用两种方法解决了问题。
摘要由CSDN通过智能技术生成

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的人告诉我正确的使用方法是什么。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值