httplib2,如何设置多个cookie? [英] httplib2, how to set more than one cookie?
问题描述
你可能知道,通常HTTP服务器不仅会发送一个session_id cookie;但是,httplib2使用字典处理cookie,如下所示:
As you are probably aware, more often than not, an HTTP server will send more than just a session_id cookie; however, httplib2 handles cookies with a dictionary, like this:
response, content = http.request(url, 'GET', headers=headers)
headers = {'Cookie': response['set-cookie']}
url = 'http://www.example.com/home'
response, content = http.request(url, 'GET', headers=headers)
那么,如何设置额外的cookie?如果使用字典处理,我不能有双重Cookie键:S。
So, how do I set the extra cookies? If handled with a dictionary, I can't have double Cookie keys :S.
感谢您的时间。
推荐答案
Cookie包含在单个HTTP标头中,用分号分隔。示例:
Cookies are contained in a single HTTP header, separated by semicolons. Example:
cookie1=value1;cookie2=value2
因此,您需要从服务器发送的Cookie构建一个字符串,然后将其设置为 Cookie
头。
So you'll need to build a string from the cookies sent by the server, and then set that as the Cookie
header.
编辑:实际上,使用httplib2进行一些操作并重新阅读你的问题,我不确定你是否真的需要任何东西来获得你想要的功能。从httplib2返回的 set-cookie
实际上是从服务器发送的原始 Set-Cookie
头;你可以把它放入新响应的 cookie
标题中,一切都会正常工作。从技术上讲,你应该删除一些cookie属性,例如 expiry
,但我想大多数服务器会处理好。
Edit: Actually, playing around a bit with httplib2 and re-reading your question, I'm not sure you actually need to do anything to get the functionality you want. The set-cookie
value you get back from httplib2 is actually the raw Set-Cookie
header sent from the server; you can just put that into the cookie
header of the new response, and everything will work fine. Technically speaking you should remove some cookie attributes such as expiry
, but I imagine most servers will handle that just fine.
这篇关于httplib2,如何设置多个cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!