简体   繁体   English

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

[英]'Missing' cookies in python requests response

I have a working cURL command, which gets a number of cookies from this particular (alfresco, although I don't think this is particularly an alfresco issue), however when I try and encode the same request with python requests, I get only one cookie. 我有一个有效的cURL命令,该命令从此特定命令中获取了许多cookie(露天,尽管我认为这不是露天问题),但是当我尝试使用python请求对同一请求进行编码时,我只会得到一个曲奇饼。

The cURL request is 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

And the python is python是

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

where 哪里

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

designed to closely match the headers that cURL is sending in. The allow_redirects=False is there to mirror the lack of the -L option. 设计用于紧密匹配cURL发送的标头allow_redirects=False可以反映缺少-L选项的情况。

cURL yields: 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

Whereas I only get the JSESSIONID cookie in requests. 而我仅在请求中获得JSESSIONID cookie。

Update I foolishly failed to include the headers sent in the cURL command, and those received. 更新我愚蠢地未能包括在cURL命令中发送的标头和收到的标头。 Those sent were 发送的是

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

And the headers sent back are 然后发送回的标头是

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

The headers sent back to requests are 发送回请求的标头是

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

The solution was to remove the "Host" header and set the referer field to be ''. 解决方案是删除“主机”标头,并将引用字段设置为“”。 I then got all the cookies I needed, so thanks to all the helpful comments. 然后,我获得了所需的所有cookie,因此感谢所有有用的评论。

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

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