#coding=utf-8
import hashlib
import re
import requests
import datetime
import os
import sys
import io
print(sys.getdefaultencoding())
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding=‘utf-8’)
account = sys.argv[1]
password = sys.argv[2]
product = sys.argv[3]
project = sys.argv[4]
bulid = sys.argv[5]
name = sys.argv[6]
print(account)
print(password)
print(product)
print(project)
print(bulid)
print(name)
#创建session
session = requests.Session()
#设置表单参数
passwordStrength = ‘1’
referer = ‘/zentao/’
keepLogin = ‘0’
frame = ‘pytest’
pri = ‘0’
status = ‘done’
#构建登陆请求头
login_url = ‘http://10.175.94.181:8080/zentao/user-login.html’
response = session.get(login_url)
html = response.text
#正则表达式,获取服务器端发来的随机数
pattern = r"<input\s+type=‘hidden’\s+name=‘verifyRand’\s+id=‘verifyRand’\s+value=‘(\d+)’.*>"
match = re.search(pattern, html)
rand = verifyRand = match.group(1)
passwordmd5 = hashlib.md5((hashlib.md5(password.encode(‘utf-8’)).hexdigest() + rand).encode(‘utf-8’)).hexdigest()
#构建参数
payload = {
‘account’:account,
‘password’:passwordmd5,
‘passwordStrength’:passwordStrength,
‘referer’:referer,
‘verifyRand’:rand,
‘keepLogin’:keepLogin,
}
response = session.post (login_url,data=payload)
print(response.status_code)
print(response.content)
#構建提交請求頭
send_url = ‘http://10.175.94.181:8080/zentao/testtask-importUnitResult-’+product+‘.html’
response = session.get(send_url)
html = response.text
#print(html)
#正則表達式,獲取bulidID,project,uid
#a = ‘NPI部门组织结构和人员信息接口_第三轮测试’
#pattern = r"<option\s+value=‘(\d+)’\s+title=‘“+bulid+”’.*>"
#match = re.search(pattern, html)
#bulid = match.group(1)
print(bulid)
#b = ‘NPI部门组织结构和人员信息接口’
#pattern = r"<option\s+value=‘(\d+)’\s+title=‘“+project+”’.*>"
#match = re.search(pattern, html)
#project = match.group(1)
print(project)
pattern = r"var\s+kuid\s+=\s+‘(.*)’"
match = re.search(pattern, html)
uid = verifyRand = match.group(1)
print(uid)
#獲取當前時間和前一天時間
now = datetime.datetime.now()
one_day = datetime.timedelta(days=-1)
one_day_before = now + one_day
begin = one_day_before.strftime(‘%Y-%m-%d’)
end = now.strftime(‘%Y-%m-%d’)
print(begin)
print(end)
#构建参数
payload = {
‘project’:project,
‘build’:bulid,
‘frame’:frame,
‘owner’:account,
‘pri’:pri,
‘begin’:begin,
‘end’:end,
‘name’:name,
‘desc’:‘’,
‘mailto[]’:‘’,
‘contactListMenu’:‘’,
‘uid’:uid,
‘product’:product,
‘status’:status,
}
headers = {
‘Content-Type’:‘application/xml’
}
#發送請求
#獲取文件
filename = ‘.\testresult.xml’
if not os.path.isfile(filename):
print(f’Error:{filename} does not exist’)
exit()
with open(filename,‘rb’) as f:
resultFile = {‘resultFile’:f}
response = session.post(send_url,data=payload,files=resultFile)
print(response.status_code)
print(response.content)
session.close()