为什么这个Curl的翻译请求不会产生相同的响应?

时间:2015-11-19 10:27:47

标签: python curl python-requests

我尝试将以下Curl命令转换为对requests.request()的调用。 Curl命令产生302响应(按预期),请求使用不同的标题项调用200响应。但我无法辨别出与众不同之处:

卷曲

curl -i 'https://some.domain.com/index/authorize?oa_token=123abc&locale=en_US' 
-H 'Pragma: no-cache' 
-H 'Accept-Encoding: gzip, deflate, sdch' 
-H 'Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4' 
-H 'Upgrade-Insecure-Requests: 1' 
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36' 
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' 
-H 'Referer: http://cool.domain.com/' 
-H 'Connection: keep-alive' 
-H 'Cache-Control: no-cache'

请求

req = requests.request("GET",
    "https://some.domain.com/index/authorize?oa_token={token}&locale=en_US".format(token="123abc"),
    headers={
        "Pragma": "no-cache",
        "Accept-Encoding": "gzip, deflate, sdch",
        "Accept-Language": "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4",
        "Upgrade-Insecure-Requests": 1,
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Referer": "http://cool.domain.com/",
        "Connection": "keep-alive",
        "Cache-Control": "no-cache"
    }
)   

任何想法可能出错?

> requests.__version__
2.2.1

> sys.version
'2.7.6 (default, Jun 22 2015, 17:58:13) \n[GCC 4.8.2]'

1 个答案:

答案 0 :(得分:1)

request.request(...)内添加以下开关allow_redirects=False以停止重定向,您将获得302状态。

仅提供信息,如果添加200切换,您还可以从curl获取-L代码。这意味着按照位置。