2010-03-16 6 views
2

urllib2를 사용하여 여러 개의 Set-Cookie 헤더를 보내는 웹 사이트와 상호 작용합니다. 그러나 응답 헤더 사전에는 하나만 들어 있습니다 - 중복 키가 서로 무시하는 것 같습니다.urllib2 여러 개의 Set-Cookie 헤더가 있습니다.

urllib2로 중복 헤더에 액세스 할 수있는 방법이 있습니까?

답변

5

urllib2 docs에 따르면 결과 URL 객체의 .headers 속성은 httplib.HTTPMessage (적어도 파이썬 문서에서는 문서화되지 않은 것으로 보입니다)입니다. 그러나

,

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 [ '설정 - 쿠키']에 접근한다면, 당신은 쉼표로 구분 된 값으로 한 세트-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' 
+0

요청시 add_header를 수행 할 때도 마찬가지입니까? –

+0

Rushabh : 아니요.하지만 쿠키와 같은 경우에는 RFC 2616 규칙을 처리하는 라이브러리가 있습니다. 'Lib/cookielib'을보세요. –

0

set-cookie도 다릅니다. RFC 6265 :

원본 서버는 여러 개의 Set-Cookie 헤더 필드를 하나의 헤더 필드로 접어서는 안됩니다 (SHOULD NOT) : .필드에서 (즉, [RFC2616]에 정의 된대로) % x2C (",") 문자가 에 의해 Set-Cookie에 사용되므로 Set-Cookie 헤더 필드의 의미가 변경 될 수 있습니다. 그러한 접힘과 충돌하는 방식.

이론적으로 이것은 버그처럼 보입니다.

관련 문제