gpt4 book ai didi

python-3.x - 我怎么能把这个 curl 转换成 Python 请求

转载 作者:行者123 更新时间:2023-12-04 01:24:58 26 4
gpt4 key购买 nike

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

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

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

我相信我一定是在转换上做错了,或者在请求结构上做错了

有什么想法吗?

简单版

它不会工作,但它演示了转换的逻辑

curl

    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 See Other,然而 Python 版本只返回 400

curl

    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)

最佳答案

问题在于您如何发送 search曲奇饼。传递一个常规的 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]>对我来说。

关于python-3.x - 我怎么能把这个 curl 转换成 Python 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35295706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com