标签:字典 json 依赖 ESS oca car 地址栏 def 上传图片
前言
在提交表单操作的时候,经常会遇到图片上传的操作,图片上传是一个单独的接口,本篇以禅道为例,介绍如何上传图片
一、上传接口
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.因为上传图片是在登录之后操作的,这个需要依赖用户登录
1 ``` 2 # coding:utf-8 3 import requests 4 5 base = ‘http://127.0.0.1:81/‘ # 禅道的服务器地址 6 7 loginUrl = base+"/zentao/user-login.html" 8 9 h = { 10 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0", 11 "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 12 "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3", 13 "Accept-Encoding": "gzip, deflate", 14 "Referer": "http://127.0.0.1/zentao/user-login.html", 15 # "Cookie": # 头部没登录前不用传cookie,因为这里cookie就是保持登录的 16 "Connection": "keep-alive", 17 "Content-Type": "application/x-www-form-urlencoded", 18 } 19 20 body = {"account":"admin", 21 "password":"e10adc3949ba59abbe56e057f20f883e", 22 "keepLogin[]":"on", 23 "referer":"http://127.0.0.1/zentao/my/" 24 } 25 26 s = requests.session() # 保持会话 27 r = s.post(loginUrl, data=body, headers=h) 28 print r.content # 打印结果看到location=‘http://127.0.0.1/zentao/my/‘说明登录成功了 29 ```
三、上传图片
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.获取上传后的地址,放到浏览器的地址栏请求下,看是不是刚才上传的图片
五、参考代码
1 # coding:utf-8 2 import requests 3 4 base = ‘http://127.0.0.1:81/‘ # 禅道的服务器地址 5 6 loginUrl = base+"/zentao/user-login.html" 7 8 h = { 9 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0", 10 "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 11 "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3", 12 "Accept-Encoding": "gzip, deflate", 13 "Referer": "http://127.0.0.1/zentao/user-login.html", 14 # "Cookie": # 头部没登录前不用传cookie,因为这里cookie就是保持登录的 15 "Connection": "keep-alive", 16 "Content-Type": "application/x-www-form-urlencoded", 17 } 18 19 body = {"account":"admin", 20 "password":"e10adc3949ba59abbe56e057f20f883e", 21 "keepLogin[]":"on", 22 "referer":"http://127.0.0.1/zentao/my/" 23 } 24 25 s = requests.session() # 保持会话 26 r = s.post(loginUrl, data=body, headers=h) 27 print r.content # 打印结果看到location=‘http://127.0.0.1/zentao/my/‘说明登录成功了 28 29 # 上传图片 30 url1 = "http://127.0.0.1:81/zentao/file-ajaxUpload-5a26aca290b59.html?dir=image" 31 32 f ={ 33 "localUrl": (None,"1.png"), 34 "imgFile": ("1.png", open("d:\\1.png", "rb"), "image/png") 35 } 36 r = s.post(url1, files=f) 37 try: 38 jpgurl = base+r.json()["url"] 39 print(u"上传图片后的url地址:%s"%jpgurl) 40 except Exception as msg: 41 print(u"返回值不是json格式:%s"%str(msg)) 42 print(r.content) 43 ```
python接口自动化14-multipart/form-data上传图片
标签:字典 json 依赖 ESS oca car 地址栏 def 上传图片
原文地址:https://www.cnblogs.com/jason89/p/9028653.html