【问题标题】:Upload without memory - stream file not possible with python-requests无内存上传 - 使用 python-requests 无法实现流文件
【发布时间】:2013-11-01 19:06:07
【问题描述】:

我正在尝试使用 python 请求上传我的大文件(大约 1 gb),但它没有流式传输 - 将其加载到内存中。

with open('file.rar','rb') as ff:
    upload = requests.post(host,files={"file": ff})

正如文档所说,我试过了:

with open('file.rar','rb') as ff:
    upload = requests.post(host,data=ff)

及其工作,但我需要修改其他 POST 字段。如何在 python-requests 中做到这一点?

【问题讨论】:

  • 我在这里遇到了完全相同的问题。

标签: python python-requests


【解决方案1】:

根据请求包的文档 http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests

下面部分说明,您需要 files= 和一些 python dict 包装器。不确定这是否是您想要的。但是这种模式允许您通过将它们放在传递给数据参数的字典中来修改其他字段,如我所见。

>>> url = 'http://httpbin.org/post'
>>> files = {'file': open('report.xls', 'rb')}

>>> r = requests.post(url, files=files)
>>> r.text
{
  ...
  "files": {
   "file": "<censored...binary...data>"
,
 ...

}

【讨论】:

    猜你喜欢
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多