前言
reuqests上传一张图片到服务器,前面已经介绍过了,那么如何在提交BUG的时候,上传附件呢?
上传附件的时候,文件的name参数名称是一样的,python里面key是不可以重复的,又如何处理参数名称相同的情况?
一、上传附件
1.以禅道提交BUG的时候上传附件为例
2.fiddler抓包看请求参数,查看到文件上传的参数如下
二、上传一个附件
1.之前学了一篇ultipart/form-data文件上传,然后学了一篇ultipart/form-data表单提交,这里文件上传,就是把两篇的知识点加起来
2.把参数分开,表单的数据用data提交,文件附件用files提交
1 ``` 2 # -----------------------前面先登录步骤省略了------------- 3 # 提交bug, 带上附件 4 url1 = host+"/zentao/bug-create-1-0-moduleID=0.html" 5 d= { 6 "product": "1", 7 "module": "0", 8 "project": "", 9 "openedBuild[]": "trunk", 10 "assignedTo": "admin", 11 "type": "codeerror", 12 "os": "all", 13 "browser": "all", 14 "color": "", 15 "title": "yoyoketang-这是一个带附件的1", 16 "severity": "3", 17 "pri": "0", 18 "steps": '<p>[步骤]</p>\ 19 <p>1、第一步点</p>\ 20 <p>2、第二步点</p>\ 21 <p>3、点三步点</p>\ 22 <p>[结果]</p>\ 23 <p><img src="data/upload/1/201712/072254170557cdn.png" alt="" /></p>\ 24 <p>[期望]</p>', 25 "story": "0", 26 "task": "0", 27 "mailto[]": "", 28 "keywords": "", 29 # "files[]": ("1.png", open("d:\\1.png", "rb"), "image/png"), 30 # "labels[]": "tu1", 31 "uid": "5a2955c884f98", 32 "case": "0", 33 "caseVersion": "0", 34 "result": "0", 35 "testtask": "0" 36 } 37 38 file = { 39 "files[]": ("1.png", open("d:\\1.png", "rb"), "image/png"), 40 "labels[]": "tu1", 41 } 42 r = s.post(url1, data=d, files=file) # 分开传 43 print r.content 44 45 ```
三、传多个附件
1 1.传多个文件的时候如下,这两个参数的name都是一样的,如果用字典去传key值,很显然python的key值是不能重复的 2 3 Content-Disposition: form-data; name="files[]"; filename="1.png" 4 Content-Type: image/png 5 6 Content-Disposition: form-data; name="files[]"; filename="2.png" 7 Content-Type: image/png 8 9 2.这时候需要换个格式,传list数据 10 ``` 11 # file = { 12 # "files[]": ("1.png", open("d:\\1.png", "rb"), "image/png"), 13 # "labels[]": "tu1", 14 # } 15 16 #-------------多个文件用list类型------------ 17 file = [ 18 ("files[]", ("2.png", open("d:\\1.png", "rb"), "image/png")), 19 ("labels[]", "tu1"), 20 ("files[]", ("2.png", open("d:\\2.png", "rb"), "image/png")), 21 ("labels[]", "tu2"), 22 ] 23 ```
3.上传之后,查看禅道上是可以提交成功的
四、参考代码
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": base+"/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":base+"/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 30 # 提交bug, 带上附件 31 url1 = base+"/zentao/bug-create-1-0-moduleID=0.html" 32 f = { 33 "product": "1", 34 "module": "0", 35 "project": "", 36 "openedBuild[]": "trunk", 37 "assignedTo": "admin", 38 "type": "codeerror", 39 "os": "all", 40 "browser": "all", 41 "color": "", 42 "title": "yoyoketang-这是一个带附件的1和2", 43 "severity": "3", 44 "pri": "0", 45 "steps": '<p>[步骤]</p>\ 46 <p>1、第一步点</p>\ 47 <p>2、第二步点</p>\ 48 <p>3、点三步点</p>\ 49 <p>[结果]</p>\ 50 <p><img src="data/upload/1/201712/072254170557cdn.png" alt="" /></p>\ 51 <p>[期望]</p>', 52 "story": "0", 53 "task": "0", 54 "mailto[]": "", 55 "keywords": "", 56 # "files[]": ("1.png", open("d:\\1.png", "rb"), "image/png"), 57 # "labels[]": "tu1", 58 "uid": "5a2955c884f98", 59 "case": "0", 60 "caseVersion": "0", 61 "result": "0", 62 "testtask": "0" 63 } 64 65 # file = { 66 # "files[]": ("1.png", open("d:\\1.png", "rb"), "image/png"), 67 # "labels[]": "tu1", 68 # } 69 70 #-------------多个文件用list类型------------ 71 file = [ 72 ("files[]", ("2.png", open("d:\\1.png", "rb"), "image/png")), 73 ("labels[]", "tu1"), 74 ("files[]", ("2.png", open("d:\\2.png", "rb"), "image/png")), 75 ("labels[]", "tu2"), 76 ] 77 78 r = s.post(url1, data=f, files=file) 79 print r.content 80 81 ```