繁体   English   中英

python中的“缺少” cookie请求响应

[英]'Missing' cookies in python requests response

我有一个有效的cURL命令,该命令从此特定命令中获取了许多cookie(露天,尽管我认为这不是露天问题),但是当我尝试使用python请求对同一请求进行编码时,我只会得到一个曲奇饼。

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

python是

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'}

解决方案是删除“主机”标头,并将引用字段设置为“”。 然后,我获得了所需的所有cookie,因此感谢所有有用的评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM