python中的'Missing'cookie请求响应

时间:2014-10-08 08:44:55

标签: python curl cookies python-requests

我有一个有效的cURL命令,从这个特定的地方获取了一些cookie(露天,虽然我不认为这特别是露天问题)但是当我尝试用python请求编码相同的请求时,我只获得一个cookie。

cURL请求

curl -v --junk-session-cookies -H 'Content-Type: application/x-www-form-urlencoded' --cookie-jar cookies.txt --cookie cookies.txt -H 'Origin: http://<url>' -D headers.txt -e ';auto' -X POST -d @credentials http://<url>/share/page/dologin

蟒蛇是

r=s.post('%s/share/page/dologin' % <url>,
           data=credentials, allow_redirects=False)

,其中

s.headers
Out[121]: {'Origin': <url>, 'Host' :<url>, 'referer':<url> , 'Accept': '*/*', 'User-Agent': 'python-requests/2.4.1 CPython/2.7.5+ Linux/3.11.0-26-generic','Content-Type': 'application/x-www-form-urlencoded'}

旨在与cURL发送的标头紧密匹配。 allow_redirects=False可以反映缺少-L选项。

cURL产量:

Added cookie JSESSIONID="<removed>" for domain 10.12.3.166, path /share/, expire 0 < Set-Cookie: JSESSIONID=<removed>; Path=/share/; HttpOnly * Added cookie alfLogin="<removed>" for domain 10.12.3.166, path /share, expire 1413289198 < Set-Cookie: alfLogin=<removed>; Expires=Tue, 14-Oct-2014 12:19:58 GMT; Path=/share * Added cookie alfUsername3="<obf>" for domain 10.12.3.166, path /share, expire 1413289198 < Set-Cookie: alfUsername3=<obf>; Expires=Tue, 14-Oct-2014 12:19:58 GMT; Path=/share

而我只在请求中获取JSESSIONID cookie。

更新 我愚蠢地没有包括cURL命令中发送的标头和收到的标头。 那些发送的是

> User-Agent: curl/7.32.0
> Host: <url>
> Accept: */*
> Referer: 
> Content-Type: application/x-www-form-urlencoded
> Origin: <url>
> Content-Length: 44

发回的标题是

HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Set-Cookie: JSESSIONID=<rem>; Path=/share/; HttpOnly
Set-Cookie: alfLogin=<rem>; Expires=Tue, 14-Oct-2014 12:19:58 GMT; Path=/share
Set-Cookie: alfUsername3=<rem>; Expires=Tue, 14-Oct-2014 12:19:58 GMT; Path=/share
Location: <url>/share

发送回请求的标头是

{'transfer-encoding': 'chunked', 'set-cookie': 'JSESSIONID=<rem>; Path=/share/; HttpOnly', 'server': 'Apache-Coyote/1.1', 'connection': 'close', 'date': 'Wed, 08 Oct 2014 08:54:53 GMT', 'content-type': 'text/html;charset=ISO-8859-1'}

1 个答案:

答案 0 :(得分:0)

解决方案是删除&#34; Host&#34;标题并将引用字段设置为&#39;&#39;。然后我得到了我需要的所有饼干,所以感谢所有有用的评论。