python接口自动化16-multipart/form-data上传图片
前言
在提交表单操作的时候,经常会遇到图片上传的操作,图片上传是一个单独的接口,本篇以禅道为例,介绍如何上传图片
上传接口
1.以禅道上提交bug为例,在选择图片时,点确定按钮,就是上传图片了
2.用fiddler抓包,查看抓到的接口,以下这种接口就是multipart/form-data
- Content-Type: multipart/form-data
- body参数是这种格式:
-----------------------------22165374713946
Content-Disposition: form-data; name="localUrl"
yoyoketang.png
-----------------------------22165374713946
Content-Disposition: form-data; name="imgFile"; filename="yoyoketang.png"
Content-Type: image/png
先登录
1.因为上传图片是在登录之后操作的,这个需要依赖用户登录
# coding:utf-8
import requests
base = 'http://127.0.0.1:81/' # 禅道的服务器地址
loginUrl = base+"/zentao/user-login.html"
h = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
"Accept-Encoding": "gzip, deflate",
"Referer": "http://127.0.0.1/zentao/user-login.html",
# "Cookie": # 头部没登录前不用传cookie,因为这里cookie就是保持登录的
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
}
body = {"account":"admin",
"password":"e10adc3949ba59abbe56e057f20f883e",
"keepLogin[]":"on",
"referer":"http://127.0.0.1/zentao/my/"
}
s = requests.session() # 保持会话
r = s.post(loginUrl, data=body, headers=h)
print r.content # 打印结果看到location='http://127.0.0.1/zentao/my/'说明登录成功了
上传图片
1.上传图片以下这种格式:
-----------------------------22165374713946
Content-Disposition: form-data; name="localUrl"
yoyoketang.png
-----------------------------22165374713946
Content-Disposition: form-data; name="imgFile"; filename="yoyoketang.png"
Content-Type: image/png
2.写成字典格式,key对应name="imgFile"这里name对应的值
3.value里面是一个元组()
- 第一个参数是filename=后面对应的值,没有就写None
- 第二个是上面的这个值“”yoyoketang.png“”,或者是文件open打开的二进制流
- 第三个是Content-Type对应的类型,如:"image/png"
f ={
"localUrl": (None,"1.png"),
"imgFile": ("1.png", open("d:\1.png", "rb"), "image/png")
}
验证是否上传成功
1.获取上传后的地址,放到浏览器的地址栏请求下,看是不是刚才上传的图片
参考代码
# coding:utf-8
import requests
base = 'http://127.0.0.1:81/' # 禅道的服务器地址
loginUrl = base+"/zentao/user-login.html"
h = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
"Accept-Encoding": "gzip, deflate",
"Referer": "http://127.0.0.1/zentao/user-login.html",
# "Cookie": # 头部没登录前不用传cookie,因为这里cookie就是保持登录的
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
}
body = {"account":"admin",
"password":"e10adc3949ba59abbe56e057f20f883e",
"keepLogin[]":"on",
"referer":"http://127.0.0.1/zentao/my/"
}
s = requests.session() # 保持会话
r = s.post(loginUrl, data=body, headers=h)
print r.content # 打印结果看到location='http://127.0.0.1/zentao/my/'说明登录成功了
# 上传图片
url1 = "http://127.0.0.1:81/zentao/file-ajaxUpload-5a26aca290b59.html?dir=image"
f ={
"localUrl": (None,"1.png"),
"imgFile": ("1.png", open("d:\\1.png", "rb"), "image/png")
}
r = s.post(url1, files=f)
try:
jpgurl = base+r.json()["url"]
print(u"上传图片后的url地址:%s"%jpgurl)
except Exception as msg:
print(u"返回值不是json格式:%s"%str(msg))
print(r.content)
---------------------------------python接口自动化完整版-------------------------
全书购买地址 https://yuedu.baidu.com/ebook/585ab168302b3169a45177232f60ddccda38e695
作者:上海-悠悠 QQ交流群:588402570
也可以关注下我的个人公众号:yoyoketang