python接口自动化18-multipart/form-data上传多个附件(form表单multipart/form-data)

网友投稿 357 2022-09-01


python接口自动化18-multipart/form-data上传多个附件(form表单multipart/form-data)

前言

reuqests上传一张图片到服务器,前面已经介绍过了,那么如何在提交BUG的时候,上传附件呢?

上传附件的时候,文件的name参数名称是一样的,python里面key是不可以重复的,又如何处理参数名称相同的情况?

上传附件

1.以禅道提交BUG的时候上传附件为例

2.fiddler抓包看请求参数,查看到文件上传的参数如下

上传一个附件

1.之前学了一篇ultipart/form-data文件上传,然后学了一篇ultipart/form-data表单提交,这里文件上传,就是把两篇的知识点加起来

2.把参数分开,表单的数据用data提交,文件附件用files提交

# -----------------------前面先登录步骤省略了-------------# 提交bug, 带上附件url1 = host+"/zentao/bug-create-1-0-moduleID=0.html"d= { "product": "1", "module": "0", "project": "", "openedBuild[]": "trunk", "assignedTo": "admin", "type": "codeerror", "os": "all", "browser": "all", "color": "", "title": "yoyoketang-这是一个带附件的1", "severity": "3", "pri": "0", "steps": '

[步骤]

\

1、第一步点

\

2、第二步点

\

3、点三步点

\

[结果]

\

\

[期望]

', "story": "0", "task": "0", "mailto[]": "", "keywords": "", # "files[]": ("1.png", open("d:\\1.png", "rb"), "image/png"), # "labels[]": "tu1", "uid": "5a2955c884f98", "case": "0", "caseVersion": "0", "result": "0", "testtask": "0" }file = { "files[]": ("1.png", open("d:\\1.png", "rb"), "image/png"), "labels[]": "tu1", }r = s.post(url1, data=d, files=file) # 分开传print r.content

传多个附件

1.传多个文件的时候如下,这两个参数的name都是一样的,如果用字典去传key值,很显然python的key值是不能重复的

Content-Disposition: form-data; name="files[]"; filename="1.png"

Content-Type: image/png

Content-Disposition: form-data; name="files[]"; filename="2.png"

Content-Type: image/png

2.这时候需要换个格式,传list数据

# file = {# "files[]": ("1.png", open("d:\\1.png", "rb"), "image/png"),# "labels[]": "tu1",# }#-------------多个文件用list类型------------file = [ ("files[]", ("2.png", open("d:\\1.png", "rb"), "image/png")), ("labels[]", "tu1"), ("files[]", ("2.png", open("d:\\2.png", "rb"), "image/png")), ("labels[]", "tu2"), ]

3.上传之后,查看禅道上是可以提交成功的

参考代码

# coding:utf-8import requestsbase = ' # 禅道的服务器地址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": base+"/zentao/user-login.html", # "Cookie": # 头部没登录前不用传cookie,因为这里cookie就是保持登录的 "Connection": "keep-alive", "Content-Type": "application/x- }body = {"account":"admin", "password":"e10adc3949ba59abbe56e057f20f883e", "keepLogin[]":"on", "referer":base+"/zentao/my/" }s = requests.session() # 保持会话r = s.post(loginUrl, data=body, headers=h)print r.content # 打印结果看到location='提交bug, 带上附件url1 = base+"/zentao/bug-create-1-0-moduleID=0.html"f = { "product": "1", "module": "0", "project": "", "openedBuild[]": "trunk", "assignedTo": "admin", "type": "codeerror", "os": "all", "browser": "all", "color": "", "title": "yoyoketang-这是一个带附件的1和2", "severity": "3", "pri": "0", "steps": '

[步骤]

\

1、第一步点

\

2、第二步点

\

3、点三步点

\

[结果]

\

\

[期望]

', "story": "0", "task": "0", "mailto[]": "", "keywords": "", # "files[]": ("1.png", open("d:\\1.png", "rb"), "image/png"), # "labels[]": "tu1", "uid": "5a2955c884f98", "case": "0", "caseVersion": "0", "result": "0", "testtask": "0" }# file = {# "files[]": ("1.png", open("d:\\1.png", "rb"), "image/png"),# "labels[]": "tu1",# }#-------------多个文件用list类型------------file = [ ("files[]", ("2.png", open("d:\\1.png", "rb"), "image/png")), ("labels[]", "tu1"), ("files[]", ("2.png", open("d:\\2.png", "rb"), "image/png")), ("labels[]", "tu2"), ]r = s.post(url1, data=f, files=file)print r.content

---------------------------------python接口自动化完整版-------------------------

作者:上海-悠悠 QQ交流群:730246532


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小案例--python编写设置拼手气红包模块
下一篇:python接口自动化19-requests-toolbelt处理multipart/form-data(python requests接口自动化测试)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~