我怎么能把这个curl转换成Python请求

时间:2016-02-09 15:23:11

标签: python-3.x curl python-requests

我可以在终端上运行以下CURL POST请求,但不能使用Python请求

我需要将CURL command转换为Python 3请求格式。

这是我将CURL命令转换为Python的方法。 正确的响应状态代码应为303,但我只能获得400

我认为我必须在转换上做错事,或者对请求结构做错了什么

有什么想法吗?

简易版

它不起作用,但它证明了转换的逻辑

卷曲

    curl -v 'https://m.flyscoot.com/select' -H 'Cookie: search="{\"origin\":\"TPE\",\"destination\":\"DMK\",\"roundTrip\":false, \"daysToSearch\":7}"; PLAY_SESSION="55158"' -H 'Connection: keep-alive' --data 'sellkey%5B0%5D=TZ~+201~INDMK' --compressed

的Python

    req = requests.session()
    postdata='sellkey%5B0%5D=TZ~+201~INDMK'
    cookies={"search":"\"{\"origin\"\"TPE\",\"destination\"\"DMK\",\"roundTrip\"false, \"daysToSearch\"7}\"","PLAY_SESSION":"\"55158\""}
    headers={"Connection":" keep-alive"}
    resp = req.post('https://m.flyscoot.com/select', cookies=cookies, headers=headers, data=postdata, allow_redirects=False)

完整版()

curl命令将返回HTTP / 1.1 303请参阅其他,但是,Python版本仅返回400

卷曲

    curl -v 'https://m.flyscoot.com/select' -H 'Cookie: optimizelyEndUserId=oeu1454030467608r0.5841516454238445; jumpseat_uid=Ah7X4ZT_NXmSLYOAYBm0dVx; granify.uuid=10080080-5828-4088-8820-108858281088; granify.lasts@1345=1454922424819; optimizelySegments=%7B%222335550040%22%3A%22gc%22%2C%222344180004%22%3A%22referral%22%2C%222354350067%22%3A%22false%22%2C%222355380121%22%3A%22none%22%7D; optimizelyBuckets=%7B%7D; __utma=185425846.1370716408.1454030468.1454922423.1454934315.7; __utmz=185425846.1454922423.6.6.utmcsr=google|utmgclid=CjwKEAiAluG1BRDrvsqCtYWk81gSJACZ2BCe5412cEmI310uK0v5JnRXLgFfPREVuw0SVs1BCx98_RoCnMnw_wcB|utmgclsrc=aw.ds|utmdclid=CJarqrDo58oCFYOMvQodqsMMXQ|utmccn=(not%20set)|utmcmd=(not%20set)|utmctr=(not%20provided); PLAY_LANG=zh-TW; _ga=GA1.2.1370716408.1454030468; search="{\"origin\":\"TPE\",\"destination\":\"DMK\",\"roundTrip\":false,\"departureDate\":\"20160405\",\"returnDate\":null,\"adults\":1,\"children\":0,\"infants\":0,\"paxResidentCountry\":null,\"currencyCode\":null,\"promoCode\":\"\",\"daysToSearch\":7}"; PLAY_SESSION="55158214a243d72b183f188b152664a332da00da-timestamp=1455030403095&signature=XoLN%2F6wa2tI%3D%7CX59B1DgzPM5q%2FGgKUkJe31k3qm%2FQ9QMtj931yzIlo79HbUzml4OUg4Q3UrQghl8PzcBpSzmUQFSkD1tFwFAvTcQ7GW5SzdBK1aRcF0QjuC48RYFTMrPRTBwhS9vz2C4fBzVnJjuUBvM%3D"' -H 'Origin: https://m.flyscoot.com' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Accept: text/html, */*; q=0.01' -H 'Referer: https://m.flyscoot.com/select' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' -H 'AlexaToolbar-ALX_NS_PH: AlexaToolbar/alxg-3.3' --data 'sellkey%5B0%5D=TZ~+201~+~~TPE~04%2F05%2F2016+15%3A35~SIN~04%2F05%2F2016+20%3A15~%5ETZ~+298~+~~SIN~04%2F06%2F2016+06%3A30~DMK~04%2F06%2F2016+08%3A25~%7C1~T~~T1TZA~3000~~107~%5E2~O1~~O1TZA~2000~~26~X%7C20160405+TZ+201+TPESIN%5E20160406+TZ+298+SINDMK' --compressed

的Python

    req = requests.session()
    postdata='sellkey%5B0%5D=TZ~+201~+~~TPE~04%2F05%2F2016+15%3A35~SIN~04%2F05%2F2016+20%3A15~%5ETZ~+298~+~~SIN~04%2F06%2F2016+06%3A30~DMK~04%2F06%2F2016+08%3A25~%7C1~T~~T1TZA~3000~~107~%5E2~O1~~O1TZA~2000~~26~X%7C20160405+TZ+201+TPESIN%5E20160406+TZ+298+SINDMK'
    cookies={"optimizelyEndUserId":"oeu1454030467608r0.5841516454238445","jumpseat_uid":"Ah7X4ZT_NXmSLYOAYBm0dVx","granify.uuid":"10080080-5828-4088-8820-108858281088","granify.lasts@1345":"1454922424819","optimizelySegments":"%7B%222335550040%22%3A%22gc%22%2C%222344180004%22%3A%22referral%22%2C%222354350067%22%3A%22false%22%2C%222355380121%22%3A%22none%22%7D","optimizelyBuckets":"%7B%7D","__utma":"185425846.1370716408.1454030468.1454922423.1454934315.7","__utmz":"185425846.1454922423.6.6.utmcsrgoogle|utmgclidCjwKEAiAluG1BRDrvsqCtYWk81gSJACZ2BCe5412cEmI310uK0v5JnRXLgFfPREVuw0SVs1BCx98_RoCnMnw_wcB|utmgclsrcaw.ds|utmdclidCJarqrDo58oCFYOMvQodqsMMXQ|utmccn(not%20set)|utmcmd(not%20set)|utmctr(not%20provided)","PLAY_LANG":"zh-TW","_ga":"GA1.2.1370716408.1454030468","search":"\"{\"origin\"\"TPE\",\"destination\"\"DMK\",\"roundTrip\"false,\"departureDate\"\"20160405\",\"returnDate\"null,\"adults\"1,\"children\"0,\"infants\"0,\"paxResidentCountry\"null,\"currencyCode\"null,\"promoCode\"\"\",\"daysToSearch\"7}\"","PLAY_SESSION":"\"55158214a243d72b183f188b152664a332da00da-timestamp1455030403095&signatureXoLN%2F6wa2tI%3D%7CX59B1DgzPM5q%2FGgKUkJe31k3qm%2FQ9QMtj931yzIlo79HbUzml4OUg4Q3UrQghl8PzcBpSzmUQFSkD1tFwFAvTcQ7GW5SzdBK1aRcF0QjuC48RYFTMrPRTBwhS9vz2C4fBzVnJjuUBvM%3D\""}
    headers={"Origin":" https//m.flyscoot.com","Accept-Encoding":" gzip, deflate","Accept-Language":" en-US,en;q=0.8","User-Agent":" Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36","Content-Type":" application/x-www-form-urlencoded; charset=UTF-8","Accept":" text/html, */*; q=0.01","Referer":" https//m.flyscoot.com/select","X-Requested-With":" XMLHttpRequest","Connection":" keep-alive","AlexaToolbar-ALX_NS_PH":" AlexaToolbar/alxg-3.3"}
    resp = req.post('https://m.flyscoot.com/select', cookies=cookies, headers=headers, data=postdata, allow_redirects=False)

1 个答案:

答案 0 :(得分:0)

问题在于您如何发送search Cookie。传递常规JSON字符串:

import json

import requests

req = requests.session()
postdata = {
    "sellkey[0]": "TZ~ 201~ ~~TPE~02/11/2016 15:45~SIN~02/11/2016 20:50~^TZ~ 302~ ~~SIN~02/12/2016 17:35~DMK~02/12/2016 19:05~|1~T~~T1TZA~3000~~107~^2~X1~~X1TZA~2000~~26~X|20160211 TZ 201 TPESIN^20160212 TZ 302 SINDMK"
}

search = {
    "origin": "TPE",
    "destination": "DMK",
    "roundTrip": False,
    "departureDate": "20160405",
    "returnDate": None,
    "adults": 1,
    "children": 0,
    "infants": 0,
    "paxResidentCountry": None,
    "currencyCode": None,
    "promoCode": "",
    "daysToSearch": 7
}
cookies = {"search": json.dumps(search),
           "PLAY_SESSION": "a40ac9ca54f044f7e802a45d48e204cd66ea5647-timestamp=1455032755654&signature=MZMKAjHPQiM%3D%7CWmWPW96Skizt%2BI7Go9fiDcTP5TI8WMQUp8BCyMA1MzNHRDCKS6vdgDPQ%2FazNt8dK3yIjYTa%2BOSMSD9tyEhzHP22cVTk%2BVXNt07hLUep%2FYomxf8jRD%2FHufKkxNnabIdsp0soTsp0uZxA%3D"}

resp = req.post('https://m.flyscoot.com/select', cookies=cookies, data=postdata, allow_redirects=False)
print(resp)

为我打印<Response [303]>