Python 官方文档:入门教程 => 点击学习
小编在最近在学习用 python 做接口测试,其中有个上传图片的接口,在网上各种搜索,各种尝试。下面是我慢慢摸索的过程,希望能帮助大家(ps:新手编写,欢迎拍砖) 1.使用poster第三方模块 如果要使用poster 则需要先 安装p
from poster.encode import multipart_encode
from poster.streamingHttp import reGISter_openers
import urllib2
# 在 urllib2 上注册 http 流处理句柄
register_openers()
# 开始对文件 "DSC0001.jpg" 的 multiart/fORM-data 编码
# "image1" 是参数的名字,一般通过 html 中的 <input> 标签的 name 参数设置
# headers 包含必须的 Content-Type 和 Content-Length
# datagen 是一个生成器对象,返回编码过后的参数
datagen, headers = multipart_encode({"image1": open("DSC0001.jpg", "rb")})
# 创建请求对象
request = urllib2.Request("http://localhost:5000/upload_image", datagen, headers)
# 实际执行请求并取得返回
print urllib2.urlopen(request).read()
data = { 'name': 'Nginx'}
files = {'file': open("abc.csv", 'rb')}
response = requests.post(url, data=data, files=files)
--结束END--
本文标题: python 模拟post上传图片
本文链接: https://www.lsjlt.com/news/191431.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0