python处理multipart/form-data的请求方法
作者:hqzxsc2006 时间:2022-01-22 22:14:11
方法1:
import requests
url = "http://www.xxxx.net/login"
#参数拼凑,附件上传格式如picurl参数,其他表单参数值拼成tuple格式:
2-tuples (filename, fileobj),
3-tuples (filename, fileobj, contentype),
4-tuples (filename, fileobj, contentype, custom_headers)
files = {"username": (None, "billy"), "password": (None, "abcd1234"),
'picUrl': ('pic.png', open('E:\\download\\pic.png', 'rb'), 'image/png')}
#如需headers,不需要赋值Content-Type,不然可能会报错
res = requests.post(url, files=files)
print res.request.body
print res.request.headers
方法2:
安装requests_toolbelt
pip install requests-toolbelt
实现代码
a.发送文件中的数据
from requests_toolbelt import MultipartEncoder
import requests
m = MultipartEncoder(
fields={'field0': 'value', 'field1': 'value',
'field2': ('filename', open('file.py', 'rb'), 'text/plain')},
)
r = requests.post('http://httpbin.org/post', data=m,
headers={'Content-Type': m.content_type})
b.不需要文件
from requests_toolbelt import MultipartEncoder
import requests
m = MultipartEncoder(fields={'field0': 'value', 'field1': 'value'})
r = requests.post('http://httpbin.org/post', data=m,
headers={'Content-Type': m.content_type})
来源:https://blog.csdn.net/hqzxsc2006/article/details/80756406
标签:python,multipart,form-data
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入浅析Python中join 和 split详解(推荐)
2022-09-19 17:43:38
JS获取网页中HTML元素的几种方法分析
2007-10-12 13:08:00
nginx简单配置多个php服务实例教程
2023-06-11 22:53:30
python装饰器底层原理详解
2021-12-31 08:48:32
![](https://img.aspxhome.com/file/2023/6/75216_0s.png)
微信小程序wx.request拦截 器使用详解
2023-07-22 09:11:55
JavaScript中两个字符串的匹配
2023-08-08 00:46:01
用ASP显示ACCESS数据库的GIF图象
2008-11-16 18:09:00
Python抢购脚本的编写方法
2021-10-03 09:47:21
![](https://img.aspxhome.com/file/2023/5/81665_0s.jpg)
pycharm 配置svn的图文教程(手把手教你)
2022-10-21 18:47:47
![](https://img.aspxhome.com/file/2023/7/83697_0s.jpg)
Python中非常实用的Math模块函数教程详解
2022-12-08 02:12:49
Python3实现自定义比较排序/运算符
2023-04-15 23:32:25
详解pandas中Series()和DataFrame()的区别与联系
2023-06-24 23:05:46
怎样在MySQL数据库中导出整个数据库
2008-12-31 15:13:00
用CSS实现柱状图(Bar Graph)的方法(一)—基于列表元素的柱状图
2008-05-26 13:03:00
![](https://img.aspxhome.com/file/UploadPic/up/2008052813514636.gif)
解决方案,而不是功能
2011-01-30 18:11:00
![](https://img.aspxhome.com/file/UploadPic/20111/30/01-63s.jpg)
Python实现EM算法实例代码
2021-05-06 03:02:26
![](https://img.aspxhome.com/file/2023/0/71500_0s.jpg)
关于 Web 字体:现状与未来[译]
2009-11-24 13:50:00
![](https://img.aspxhome.com/file/UploadPic/200911/24/3739976284_7943de8cbe-34s.jpg)
Python编程matplotlib绘图挑钻石seaborn小提琴和箱线图
2022-03-06 20:08:29
![](https://img.aspxhome.com/file/2023/7/92177_0s.png)
对用户研究实践的思考
2010-10-19 12:21:00
![](https://img.aspxhome.com/file/UploadPic/201010/19/01-90s.jpg)
python基础教程之基本数据类型和变量声明介绍
2023-08-28 22:45:55