使用python请求模块在单个请求中上传多个文件 [英] Uploading multiple files in a single request using python requests module
本文介绍了使用python请求模块在单个请求中上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Python requests 模块 提供了关于如何在单个请求:
The Python requests module provides good documentation on how to upload a single file in a single request:
files = {'file': open('report.xls', 'rb')}
我尝试通过使用此代码来扩展该示例以尝试上传多个文件:
I tried extending that example by using this code in an attempt to upload multiple files:
files = {'file': [open('report.xls', 'rb'), open('report2.xls, 'rb')]}
但它导致了这个错误:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 1052, in splittype
match = _typeprog.match(url)
TypeError: expected string or buffer
是否可以使用此模块在单个请求中上传文件列表,以及如何上传?
Is it possible to upload a list of files in a single request using this module, and how?
推荐答案
要在单个请求中上传具有相同键值的文件列表,您可以创建一个元组列表,以每个元组中的第一项作为键值和文件对象作为第二个:
To upload a list of files with the same key value in a single request, you can create a list of tuples with the first item in each tuple as the key value and the file object as the second:
files = [('file', open('report.xls', 'rb')), ('file', open('report2.xls', 'rb'))]
这篇关于使用python请求模块在单个请求中上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文