首页 > 解决方案 > 在 python 中通过 requests.put 上传文件

问题描述

我正在尝试使用 requests 模块将以下 curl 命令转换为 python 代码。

curl -v -X PUT -T video_file.mp4 https://my-app-domain.com

已经尝试了以下一些方法,但仍然无法正常工作。

with open(mp4_file_path, 'rb') as finput:
     response = requests.put('https://my-app-domain.com', data=finput)
       

有人可以告诉我怎么写吗?先感谢您。

标签: pythoncurlpython-requests

解决方案


根据此处的文档,数据可以接受“字典、元组列表、字节或类似文件的对象”。

这应该有效:

with open(mp4_file_path, 'rb') as finput:
    response = requests.put('https://my-app-domain.com', data=finput.read())

推荐阅读