本文摘自php中文网,作者不言,侵删。
这篇文章主要介绍了关于python实现requests发送/上传多个文件的示例,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下1、需要的环境
Python2.X
Requests 库
2、单字段发送单个文件
在requests中发送文件的接口只有一种,那就是使用requests.post的files参数, 请求形式如下:
1 2 3 4 | url = "http://httpbin.org/post"
data = None
files = { ... }
r = requests.post(url, data, files=files)
|
而这个files参数是可以接受很多种形式的数据,最基本的2种形式为:
字典类型
元组列表类型
2.1、字典类型的files参数
官方推荐使用的字典参数格式如下:
1 2 3 4 5 | {
"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元组;
这个元组的每一个字段代表的意思一次为:
1 | ( "filename" , "fileobject" , "content-type" , "headers" )
|
缺省的话则会使用默认值
除了上面的使用形式,其实requests还是支持一个更简洁的参数形式,如下
1 2 3 4 5 | {
"field1" : open( "filePath1" , "rb" )),
"field2" : open( "filePath2" , "rb" )),
"field3" : open( "filePath3" , "rb" ))
}
|
这种形式的参数其等同效果如下, 其中filename是filepath的文件名:
1 2 3 4 5 | {
"field1" : ( "filename1" , open( "filePath1" , "rb" )),
"field2" : ( "filename2" , open( "filePath2" , "rb" )),
"field3" : ( "filename3" , open( "filePath3" , "rb" ))
}
|
当然,你还可以这样发送一个文件请求
1 2 3 | {
"field1" : open( "filePath1" , "rb" ).read())
}
|
这里的filename的值为field1
2.2、元组列表类型的files参数
其实元组列表的形式与字典的形式基本一样,除了最外层的包装不一样;而在requests内部最终会把字典参数形式 转换 为 元组列的形式。官网推荐的用法如下:
1 2 3 4 5 | [
( "field1" : ( "filename1" , open( "filePath1" , "rb" ))),
[ "field2" : ( "filename2" , open( "filePath2" , "rb" ), "image/jpeg" )],
( "field3" : ( "filename3" , open( "filePath3" , "rb" ), "image/jpeg" , { "refer" : "localhost" }))
]
|
列表里面的子项可以是元组,也可以是列表;同样这里也支持简介的形式,如下:
1 2 3 4 | [
( "field1" : open( "filePath1" , "rb" ))), ##filename 使用的是filepath的文件名
( "field2" : open( "filePath2" , "rb" ).read())) ##filename 使用的是键值,即 field2
]
|
3、单字段发送多个文件【即上传文件时,设置为多选了】
3.1、字典参数形式
1 2 3 4 5 6 7 8 | {
"field1" : [
( "filename1" , open( "filePath1" , "rb" )),
( "filename2" , open( "filePath2" , "rb" ), "image/png" ),
open( "filePath3" , "rb" ),
open( "filePath4" , "rb" ).read()
]
}
|
3.2、元组列表形式
1 2 3 4 5 6 | [
( "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参数中即可,如下:
1 2 3 4 5 | data = { "k1" : "v1" }
files = {
"field1" : open( "1.png" , "rb" )
}
r = requests.post( "http://httpbin.org/post" , data, files=files)
|
相关推荐:
python实现超简单的视频对象提取功能
Python实现ping指定IP的示例
以上就是python实现requests发送/上传多个文件的示例的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python中yaml配置文件模块的使用详解
Python中range()函数和list的比较
Python idle怎么用
Python诞生于什么时候
使用jupyter notebook 学习 Python
Python中base64加密解密操作方法及版本间差异
Python之实现自定义分页功能
Python中x的平方怎么写
r在Python中表示什么意思
Python元组创建赋值以及更新删除操作的实例分析
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python实现requests发送/上传多个文件的示例