响应中的urllib2多个Set-Cookie标头
作者:互联网
我正在使用urllib2与与发送回多个Set-Cookie标头的网站进行交互.但是,响应标头字典仅包含一个-似乎重复的键相互覆盖.
有没有办法使用urllib2访问重复的标头?
解决方法:
根据urllib2 docs,结果URL对象的.headers属性是httplib.HTTPMessage(至少在Python文档中似乎没有记录).
然而,
help(httplib.HTTPMessage)
...
If multiple header fields with the same name occur, they are combined
according to the rules in RFC 2616 sec 4.2:
Appending each subsequent field-value to the first, each separated
by a comma. The order in which header fields with the same field-name
are received is significant to the interpretation of the combined
field value.
因此,如果您访问u.headers [‘Set-Cookie’],则应该获得一个Set-Cookie标头,其值之间用逗号分隔.
确实,情况确实如此.
import httplib
from StringIO import StringIO
msg = \
"""Set-Cookie: Foo
Set-Cookie: Bar
Set-Cookie: Baz
This is the message"""
msg = StringIO(msg)
msg = httplib.HTTPMessage(msg)
assert msg['Set-Cookie'] == 'Foo, Bar, Baz'
标签:setcookie,urllib2,header,python 来源: https://codeday.me/bug/20191024/1918511.html