[英]Is it possible to set headers inside an Apache output filter?
我们将Apache 2.4与mod_python结合使用,该模块用于输出过滤器,用于重写一些HTML输出。 我们目前正在JS中使用document.cookie设置cookie,但这并不是最佳选择。 理想情况下,我们希望通过标题设置cookie。 我们尝试使用filter.req.headers_out['SetCookie']
和Cookie.add_cookie
,但无济于事。
这有可能吗? 如果没有,还有什么更好的选择? 我们只能使用Apache 2.4和mod_python。
可用的Apache模块:
加载的模块:
我目前如何尝试设置Cookie(在开发环境中):
def add_cookie(req, name, value, domain=None, expires=None):
"""Adds a cookie
Arguments:
req -- the request
name -- the cookie name
value -- the cookie value
domain -- (optional) the domain the cookie is applicable to
expires -- (optional) the time in minutes the cookie is set to expire, defaults to current session
"""
cookie = Cookie.Cookie(name, value)
# Always set the path to the root
cookie.path = '/'
# Set domain if present
if domain is not None:
cookie.domain = domain
# Set expires if present
if expires is not None:
expires = int(expires)
cookie.expires = time.time() + (60 * expires)
# Add a freshly-baked cookie
Cookie.add_cookie(req, cookie)
我自己解决了这个问题。 简短的版本是,可以。 之前对我不起作用的原因是我设置Cookie的位置不正确。 我从HTML处理区域(无论如何不属于该区域)移动了这一位,并将其直接放在outputfilter
方法中。
我希望这可以帮助别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.