在GAE中尝试使用Set-Cookie添加标头时出错

Ser*_*rov 3 python session google-app-engine webapp2

我试图在我的项目中包含外部python模块来处理会话.它被命名了gmemsess.py.它尝试在响应中添加Set-Cookie标头,并显示错误:

rh.response.headers.add_header('Set-Cookie','%s=%s; path=/;'%(name,self._sid))
AttributeError: HeaderDict instance has no attribute 'add_header'
Run Code Online (Sandbox Code Playgroud)

我阅读文档,一切似乎都没问题,但它不起作用.为什么会出现此错误?此外,我使用webapp2来管理子域.因为这个可能会出现问题吗?

Ada*_*and 5

headers.add_header如果您正在使用库存AppEngine,该方法应该绝对有效,但我猜您正在使用框架 - 并且有很多它们,如Bottle,它使用webob的Response对象的自定义替换.

谷歌的一点时间显示,至少有一个名为HeaderDict的可识别类扩展了MultiDict,我认为这就是你正在处理的问题.在这种情况下,您应该进入gmemsess.py并更改该行

rh.response.headers.add_header('Set-Cookie','%s=%s; path=/;'%(name,self._sid))
Run Code Online (Sandbox Code Playgroud)

阅读

rh.response.headers['Set-Cookie'] = '%s=%s; path=/;'%(name,self._sid)
Run Code Online (Sandbox Code Playgroud)

这应该可以解决你的问题.