Cookiejar 在开瓶器中的使用
我现在有以下代码:
tw_jar = cookielib.CookieJar()
tw_jar.set_cookie(c1)
tw_jar.set_cookie(c2)
o = urllib2.build_opener( urllib2.HTTPCookieProcessor(tw_jar) )
urllib2.install_opener( o )
现在我稍后在代码中我不想使用任何 cookie(同时还创建了新的 cookie)。
我可以执行一个简单的 tw_jar.clear()
还是需要再次构建并安装 opener 以删除请求中使用的所有 cookie?
I have the following code at the moment:
tw_jar = cookielib.CookieJar()
tw_jar.set_cookie(c1)
tw_jar.set_cookie(c2)
o = urllib2.build_opener( urllib2.HTTPCookieProcessor(tw_jar) )
urllib2.install_opener( o )
Now I later in my code I don't want to use any of the cookies (Also new cookies created meanwhile).
Can I do a simple tw_jar.clear()
or do I need to build and install the opener again to get rid of all cookies used in the requests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我的 Python 安装中定义
HTTPCookieProcessor
的方式:由于仅保存引用,因此您只需操作原始
tw_jar
实例即可,它将影响所有将来的请求。This is how
HTTPCookieProcessor
is defined in my Python installation:As only a reference is saved, you can just manipulate the original
tw_jar
instance and it will affect all future requests.如果您不需要任何 cookie,我建议您创建一个新的 opener。然而,如果由于某种原因你想保留旧的,从处理程序列表中删除 cookie 处理器应该可以:
If you don't want any cookies, I'd recommend to create a new opener. However, if for some reason you want to keep the old one, removing the cookie processor from the list of handlers should work: