python接口自动化(十五) multipart/form-data上传多个附件

前言

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

上传附件

OPMS——员工相册上传图片,提示成功,访问响应中的url也可以访问到该图片,web页面和数据库却没有该条数据;无解ing

 

 

 -------------------------------------------------------------------------------------------------------------------------------

禅道项目

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

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

 上传一个附件

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

 传多个附件

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

 

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

 

 3.上传之后,查看禅道上的图片;

 

 参考代码:

# coding:utf-8
import requests
import re
import hashlib
pw="P@ssw0rd"
s=requests.Session()
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"
}
vrand=0
while(True):
    rs1=s.get("http://localhost/zentaopms/www/user-login.html",headers=headers)
    rs1.encoding='utf-8'
    #print(rs1.text)
    rand=re.findall(r"'verifyRand' value='(.+?)'",rs1.text)
    #print(rand[0])
    if len(rand[0])==10:
        vrand=rand[0]
        break
print(vrand)
#方式一
hash=hashlib.md5()
hash.update(pw.encode('utf-8'))
f=hash.hexdigest()+vrand
#print(f)
#方式二
hash2=hashlib.md5(f.encode('utf-8'))
pwd=hash2.hexdigest()
print(pwd)
data={
"account":"fuhui",
"password":pwd,
"referer":"http://localhost/zentaopms/www/bug-browse-6.html",
"verifyRand":vrand
}
rs2=s.post("http://localhost/zentaopms/www/user-login.html",headers=headers,data=data)
rs2.encoding='utf-8'
#print(rs2.text)
rs3=s.get("http://localhost/zentaopms/www/bug-browse-6.html",headers=headers)
rs3.encoding='utf-8'
#print(rs3.text)
result=re.findall(r"\<a href=\'\/zentaopms\/www\/user-logout.html' \>(.+?)\<\/a\>",rs3.text)
print(result)
if result[0]=="退出":
    print("登录成功")

#-----------------上传附件-------------------------

url2="http://localhost/zentaopms/www/bug-create-6-0-moduleID=0.html"
d2={"product":"6",
   "module":"0",
   "project":"5",
   "openedBuild[]":"trunk",
   "assignedTo":"huyongqin",
   "type":"codeerror",
   "title":"python学习上传多图",
   "severity":"3",
   "pri":"3",
   "steps":"<p>[步骤]</p><br/><p>[结果]</p><br/><p>[期望]</p><br/>",
   "oldTaskID":"0",
    "uid":"5f2a0514c39db",
   "case":"0",
   "caseVersion":"0",
   "result":"0",
   "testtask":"0",
    # "labels[]":"2.png"
}
# file={
#     "files[]":("2.png",open("2.png","rb"),"image/jpeg")
# }
file={
    ("files[]",("2.png",open("2.png","rb"),"image/jpeg")),
    ("labels[]","2.png"),
    ("files[]",("qq.png",open("E:\\qq.png","rb"),"image/jpeg")), #图片与当前脚本非同一目录,需要写路径目录
    ("labels[]","qq.png"),
}
result2=s.post(url2,data=d2,headers=headers,files=file,allow_redirects=False)
result2.encoding="utf-8"
print(result2.content)

 运行结果

 

posted on 2020-08-05 10:48  星空6  阅读(1141)  评论(0编辑  收藏  举报

导航