请求toolbelt上传一个巨大的文件

时间:2015-02-18 17:20:02

标签: python django python-requests bottle

我试图使用请求工具带上传一个巨大的文件。使用Python Bottle框架从HTML表单中收集文件

uploadfile = request.files.get('File Upload')

payload = MultipartEncoder({uploadfile.filename:uploadfile.file.read()})

r = requests.post(url,
                files=payload,auth=(username,password))

错误

TypeError: 'MultipartEncoder' object is not iterable

问题: 1.如何发送收集的文件对象?

1 个答案:

答案 0 :(得分:1)

使用MultipartEncoder时,我会将信息作为帖子data属性发送,如下例所示:

https://pypi.python.org/pypi/requests-toolbelt/0.3.0

以下是我将使用的示例脚本:

from requests_toolbelt import MultipartEncoder
import requests

uploadfile = open("./example_upload.dat", "r")
payload = MultipartEncoder({uploadfile.name: uploadfile})

r = requests.post(
    "http://example.com",
    data=payload,
    auth=("exampleuser","examplepassword"),
    headers={"Content-Type": payload.content_type})