如何使用Python请求模拟我在curl中进行的POST请求?

2024-10-01 17:32:25 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的Mac上,我可以使用curl发送以下请求

curl -v -X POST -d MAX_FILE_SIZE=10485760 -d 'url=https://i.imgur.com/Imox74B.gifv' http://karmadecay.com/index/

这将导致302重定向。如何在Python中复制此功能?我试过下面的方法

import requests
...
resp = requests.post(SEARCH_URL, params={"MAX_FILE_SIZE": "10485760", "url": "https://i.imgur.com/Imox74B.gifv"}, headers={"User-Agent": "curl/7.54.0"})
print(resp)

但结果是500条回复,这让我觉得我没有很好地模仿这种行为


Tags: httpscomhttpurlsizemaccurlpost
1条回答
网友
1楼 · 发布于 2024-10-01 17:32:25

params用于指定查询字符串;您的调用相当于类似SEARCH_URL + '?MAX_FILE_SIZE=10485760&url=https...'的URL。您想改用data关键字参数

resp = requests.post(
    SEARCH_URL, 
    data={
        "MAX_FILE_SIZE": "10485760",
        "url": "https://i.imgur.com/Imox74B.gifv"
    },
    headers={"User-Agent": "curl/7.54.0"},
    allow_redirects=False  # Remove this to follow redirects automatically
)

相关问题 更多 >

    热门问题