这篇文章主要介绍了关于python实现requests发送/上传多个文件的示例,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
1、需要的环境
Python2.X
Requests 库
2、单字段发送单个文件
在requests中发送文件的接口只有一种,那就是使用requests.post的files参数, 请求形式如下:
url = "http://httpbin.org/post" data = None files = { ... } r = requests.post(url, data, files=files)
而这个files参数是可以接受很多种形式的数据,最基本的2种形式为:
字典类型
元组列表类型
2.1、字典类型的files参数
官方推荐使用的字典参数格式如下:
{ "field1" : ("filename1", open("filePath1", "rb")), "field2" : ("filename2", open("filePath2", "rb"), "image/jpeg"), "field3" : ("filename3", open("filePath3", "rb"), "image/jpeg", {"refer" : "localhost"}) }
这个字典的key就是发送post请求时的字段名, 而字典的value则描述了准备发送的文件的信息;从上面可以看出value可以是2元组,3元组或4元组;
这个元组的每一个字段代表的意思一次为:
("filename", "fileobject", "content-type", "headers")
缺省的话则会使用默认值
除了上面的使用形式,其实requests还是支持一个更简洁的参数形式,如下
{ "field1" : open("filePath1", "rb")), "field2" : open("filePath2", "rb")), "field3" : open("filePath3", "rb")) }
这种形式的参数其等同效果如下, 其中filename是filepath的文件名:
{ "field1" : ("filename1", open("filePath1", "rb")), "field2" : ("filename2", open("filePath2", "rb")), "field3" : ("filename3", open("filePath3", "rb")) }
当然,你还可以这样发送一个文件请求
{ "field1" : open("filePath1", "rb").read()) }
这里的filename的值为field1
2.2、元组列表类型的files参数
其实元组列表的形式与字典的形式基本一样,除了最外层的包装不一样;而在requests内部最终会把字典参数形式 转换 为 元组列的形式。官网推荐的用法如下:
[ ("field1" : ("filename1", open("filePath1", "rb"))), ["field2" : ("filename2", open("filePath2", "rb"), "image/jpeg")], ("field3" : ("filename3", open("filePath3", "rb"), "image/jpeg", {"refer" : "localhost"})) ]
列表里面的子项可以是元组,也可以是列表;同样这里也支持简介的形式,如下:
[ ("field1" : open("filePath1", "rb"))), ##filename 使用的是filepath的文件名 ("field2" : open("filePath2", "rb").read())) ##filename 使用的是键值,即 field2 ]
3、单字段发送多个文件【即上传文件时,设置为多选了】
3.1、字典参数形式
{ "field1" : [ ("filename1", open("filePath1", "rb")), ("filename2", open("filePath2", "rb"), "image/png"), open("filePath3", "rb"), open("filePath4", "rb").read() ] }
3.2、元组列表形式
[ ("field1" , ("filename1", open("filePath1", "rb"))), ("field1" , ("filename2", open("filePath2", "rb"), "image/png")), ("field1" , open("filePath3", "rb")), ("field1" , open("filePath4", "rb").read()) ]
上面2种形式发送的请求,所有的文件都会在同一个字段下,后台服务只要从field1字段就可以获取全部的文件对象
4、同时发送普通数据字段
上面介绍的是使用发送文件内容请求,而有时候我们在发送文件的同时还需要发送普通的数据字段,此时普通数据字段直接存在data参数中即可,如下:
data = {"k1" : "v1"} files = { "field1" : open("1.png", "rb") } r = requests.post("http://httpbin.org/post", data, files=files)
相关推荐:
以上是python实现requests发送/上传多个文件的示例的详细内容。更多信息请关注PHP中文网其他相关文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

数组的同质性对性能的影响是双重的:1)同质性允许编译器优化内存访问,提高性能;2)但限制了类型多样性,可能导致效率低下。总之,选择合适的数据结构至关重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,内存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版