Python-将set-cookies响应转换为cookie的dict [英] Python - convert set-cookies response to dict of cookies
本文介绍了Python-将set-cookies响应转换为cookie的dict的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从 httplib2
响应中转换 response ['set-cookie']
输出字符串,例如
How to convert the response['set-cookie']
output string from httplib2
response like
"cookie1=xxxyyyzzz;Path=/;Expires=Wed, 03-Feb-2015 08:03:12 GMT;Secure;HttpOnly, cookie2=abcdef;Path=/;Secure"
到
{'cookie1':'xxxyyyzzz','cookies2':'abcdef'}
推荐答案
使用 http.cookies
:
>>> c = "cookie1=xxxyyyzzz;Path=/;Expires=Wed, 03-Feb-2015 08:03:12 GMT;Secure;HttpOnly, cookie2=abcdef;Path=/;Secure"
>>> from http.cookies import SimpleCookie
>>> cookie = SimpleCookie()
>>> cookie.load(c)
>>> cookie
<SimpleCookie: cookie1='xxxyyyzzz' cookie2='abcdef'>
>>> {key: value.value for key, value in cookie.items()}
{'cookie1': 'xxxyyyzzz', 'cookie2': 'abcdef'}
这篇关于Python-将set-cookies响应转换为cookie的dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文