import requests

def sendImg(img_path, img_name, img_type='image/jpeg'):
"""
:param img_path:图片的路径
:param img_name:图片的名称
:param img_type:图片的类型,这里写的是image/jpeg,也可以是png/jpg
"""
url = 'https://www.xxxxxxxxxx.com' # 自己想要请求的接口地址

with open(img_path + img_name, "rb")as f_abs:# 以2进制方式打开图片
body = {
# 有些上传图片时可能会有其他字段,比如图片的时间什么的,这个根据自己的需要

'camera_code': (None, "摄像头1"),

'image_face': (img_name, f_abs, img_type)
# 图片的名称、图片的绝对路径、图片的类型(就是后缀)

"time":(None, "2019-01-01 10:00:00")

}
# 上传图片的时候,不使用data和json,用files
response = requests.post(url=url, files=body).json
return response

if __name__=='__main__':
# 上传图片
res = sendImg(img_path, img_name) # 调用sendImg方法
print(res)

**如果上传图片是数组时,value直接写图片路径就可以**

#---------------------------------------------------------------------------------------------------------------------------------------------------------------------

# "Content-Type": "multipart/form-data; boundary=76a22e30da2bb7790828887966871012"
from urllib3 import encode_multipart_formdata
import requests

def sendFile(filename, file_path):
"""
:param filename:文件的名称
:param file_path:文件的绝对路径
"""
url = "https://www.xxxxxxx.com" # 请求的接口地址
with open(file_path, mode="r", encoding="utf8")as f: # 打开文件
file = {
"file": (filename, f.read()),# 引号的file是接口的字段,后面的是文件的名称、文件的内容
"key": "value", # 如果接口中有其他字段也可以加上
}

encode_data = encode_multipart_formdata(file)

file_data = encode_data[0]
# b'--c0c46a5929c2ce4c935c9cff85bf11d4\r\nContent-Disposition: form-data; name="file"; filename="1.txt"\r\nContent-Type: text/plain\r\n\r\n...........--c0c46a5929c2ce4c935c9cff85bf11d4--\r\n

headers_from_data = {
"Content-Type": encode_data[1],
"Authorization": token
}
# token是登陆后给的值,如果你的接口中头部不需要上传字段,就不用写,只要前面的就可以
# 'Content-Type': 'multipart/form-data; boundary=c0c46a5929c2ce4c935c9cff85bf11d4',这里上传文件用的是form-data,不能用json

response = requests.post(url=url, headers=headers_from_data, data=file_data).json()
return response

if __name__=='__main__':
# 上传文件
res = sendFile(filename, file_path) # 调用sendFile方法
print(res)

1、https://blog.csdn.net/xy_best_/article/details/92839653

2、https://www.jianshu.com/p/231e4773f7f3

3、https://www.jianshu.com/p/0023bb7afddb

multipart_formdata的更多相关文章

  1. 打造适用于c#的feign

    之前因为工作原因使用spring cloud全家桶开发过若干项目,发现其中的feign非常好用,以前开发接口客户端的时候都是重复使用HttpClient实现业务,每次新增接口都十分繁琐,故萌生了自定义 ...

随机推荐

  1. 图论--最短路--第K短路(IDA*)(IDA Star)模板

    #include <iostream> #include <cstdio> #include <cstring> #include <queue> us ...

  2. muduo网络库源码学习————条件变量

    muduo里的CountDownLatch类实际上是对条件变量condition进行的封装,既可以用于所有子线程等待主线程发起 "起跑" ,也可以用于主线程等待子线程初始化完毕才开 ...

  3. STL之内存管理

    STL以泛型思维为基础,提供了6大组件:容器(containers).算法(algorithms).迭代器(iterators).仿函数(functors).适配器(adapters).分配器(all ...

  4. 软件——IDEA 超实用使用技巧分享

    前言 工欲善其事 ​必先利其器 最近受部门的邀请,给入职新人统一培训IDEA,发现有很多新人虽然日常开发使用的是IDEA,但是还是很多好用的技巧没有用到,只是用到一些基本的功能,蛮浪费IDEA这个优秀 ...

  5. C. Fountains

    \(整体思路没错,但是我貌似太麻烦了.......\) \(分情况讨论\) \(Ⅰ.coin和diamond各选一个物品,这个简单\) \(Ⅱ.在coin中选两个或者在diamond选两个\) \(开 ...

  6. 003_python的str切片,str常用操作方法,for循环,集合,深浅copy

    基础数据类型 基础数据类型,有7种类型,存在即合理. 1.int 整数 主要是做运算的 .比如加减乘除,幂,取余  + - * / ** %... 2.bool布尔值 判断真假以及作为条件变量 3.s ...

  7. ActiveMQ 持久订阅者,执行结果与初衷相违背,验证离线订阅者无效,问题解决

    导读 最新在接触ActiveMQ,里面有个持久订阅者模块,功能是怎么样也演示不出来效果.配置参数比较简单(配置没几个参数),消费者第一次运行时,需要指定ClientID(此时Broker已经记录离线订 ...

  8. @Resource、@Autowired等几个注解的区别

    1.@Resource注解和@Autowired的区别 @Autowired注解:是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false ...

  9. shell 光标处理快捷键

    Ctrl+左右键 单词之间跳转Ctrl+a跳到本行的行首, Ctrl+e则跳到页尾. Ctrl+u删除当前光标前面的文字 ctrl+k-删除当前光标后面的文字 Ctrl+w和Alt+d-对于当前的单词 ...

  10. Mysql 常用函数(5)- substring 函数

    Mysql常用函数的汇总,可看下面系列文章 https://www.cnblogs.com/poloyy/category/1765164.html substring 的作用 截取指定范围的字符串, ...