Python接口自动化之文件上传/下载接口怎么实现
443
2022-09-01
python接口自动化19-requests-toolbelt处理multipart/form-data(python requests接口自动化测试)
requests-toolbelt
1.官方文档地址:requests-toolbelt官方文档
2.环境安装
pip install requests-toolbelt
multipart/form-data传文件
from requests_toolbelt import MultipartEncoderimport requestsm = MultipartEncoder( fields={'field0': 'value', 'field1': 'value', 'field2': ('filename', open('file.py', 'rb'), 'text/plain')} )r = requests.post(' data=m, headers={'Content-Type': m.content_type})
multipart/form-data表单提交(传非文件)
from requests_toolbelt import MultipartEncoderimport requestsm = MultipartEncoder( fields={'field0': 'value', 'field1': 'value'})r = requests.post(' data=m, headers={'Content-Type': m.content_type})
多个文件参数重复
1.参数重复的时候,就不用字典了,用list类型
from requests_toolbelt import MultipartEncoderimport requestsm = MultipartEncoder( fields = [ ('source', ('f1.ext', f1, 'application/x-example-mimetype'), ('source', ('f2.ext', f2, 'application/x-example-mimetype'),] )r = requests.post(' data=m, headers={'Content-Type': m.content_type})
参考代码:
# coding:utf-8import requestsfrom requests_toolbelt import MultipartEncoderhost = ' # 禅道的服务器地址def login(s,user="admin",psw="e10adc3949ba59abbe56e057f20f883e"): u'''登录禅道''' loginUrl = host+"/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": host+"/zentao/user-login.html", # "Cookie": # 头部没登录前不用传cookie,因为这里cookie就是保持登录的 "Connection": "keep-alive", "Content-Type": "application/x- } body = {"account": user, "password": psw, "keepLogin[]": "on", "referer": host+"/zentao/my/" } try: r = s.post(loginUrl, data=body, headers=h) print(r.content) # 打印结果看到location=' if "/zentao/my/" in r.content: print("登录成功!") return True else: print("登录失败:%s" % r.content) return False except Exception as msg: print("登录失败:%s"%str(msg)) return Falsedef upload_jpg(s): u'''上传图片''' url1 = host+"/zentao/file-ajaxUpload-5a26aca290b59.html?dir=image" m = MultipartEncoder( fields={ "localUrl": (None, "1.png"), "imgFile": ("1.png", open("d:\\1.png", "rb"), "image/png") }) try: r1 = s.post(url1, data=m, headers={'Content-Type': m.content_type}) jpg_url = r1.json()["url"] return jpg_url except Exception as msg: print("上传失败:%s"%str(msg)) return ""def submit_bug(s,jpg_url,title="yoyoketang-这是一个带附件的内容"): # 提交bug, 带上附件 url2 = host+"/zentao/bug-create-1-0-moduleID=0.html" m = MultipartEncoder( fields = [ ("product", "1"), ("module","0"), ("project", ""), ("openedBuild[]", "trunk"), ("assignedTo", "admin"), ("type", "codeerror"), ("os", "all"), ("browser", "all"), ("color", ""), ("title", title), # bug标题参数化 ("severity", "3"), ("pri", "0"), ("steps", u'
[步骤]
\1、第一步点
\2、第二步点
\3、点三步点
\[结果]
\ \[期望]
' % jpg_url), # 正文图片地址参数 ("story", "0"), ("task", "0"), ("mailto[]", ""), ("keywords", ""), # 这里的四个参数就是上传文件附件了 ("files[]", ("1.png", open("d:\\1.png", "rb"), "image/png")), # 附件1 ("labels[]", "tu1"), ("files[]", ("2.png", open("d:\\2.png", "rb"), "image/png")), # 附件1 ("labels[]", "tu2"), ("uid", "5a2955c884f98"), ("case", "0"), ("caseVersion", "0"), ("result", "0"), ("testtask", "0") ]) try: r2 = s.post(url2, data=m, headers={'Content-Type': m.content_type}) print r2.content except Exception as msg: print("提交BUG失败:%s"%str(msg))if __name__ == "__main__": s = requests.session() # 保持会话 login(s, user="admin", psw="e10adc3949ba59abbe56e057f20f883e") # 登录 jpg = upload_jpg(s) # 上传图 # 获取上传图片后用的url地址传给提交bug的正文 submit_bug(s, jpg, title="yoyoketang-这是一个带附件的内容")QQ交流群:730246532
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~