简单实现将接口返回的数据写入文本,从文本读取参数,实现接口参数自动化

import requests
import json
import datetime
import re


# 需要测试的环境
api_host = "192.168.10.XX:XXXX"
#发货宝登录 15023621999
headers_null = { 'Content-Type' : 'application/json'}
url_login = "http://" + api_host + "/ms-common-user/user/login"
data_login_fhb = {"phone":"15023621999","pwd":"123456","projectType":"fhbMant"}
request_login_fhb = requests.request("POST", url=url_login, data=json.dumps(data_login_fhb), headers=headers_null)
response=request_login_fhb.json()
fhb_token=response['data']['token']

#发货宝账号:15023621999
headers1 = {
'Content-Type' : 'application/json',
'token':fhb_token,
'pageNum':'1',
'pageSize':'20'
}

class test_write_value(object):
def get_value(self):
self.url="http://"+api_host+'/ms-fahuobao-json-view/FhbOrder/findFhbOrderAll'
res=requests.get(self.url,params="",headers=headers1)
list_str=res.json()
# order_no=list_str['data'][0]['orderNo']
list_order_no=[]
list_order_id=[]
for order_no in range(len(list_str['data'][::])): #获取循环次数为data list的长度
# list_order_no.append(list_str['data'][order_no]['orderNo']+'\n') #将数据添加至列表时追加换行符
list_order_id.append(list_str['data'][order_no]['id'] + '\n') # 将数据添加至列表时追加换行符
# print(list_order_no)
print(list_order_id)
return list_order_id #返回list_order_no
def write_value_to_txt(self):
global file_path
time=datetime.datetime.now()
name=str(str(time.year)+"-"+str(time.month)+"-"+str(time.day)+"-"+str(time.hour)+"-"+str(time.minute)+"-"+str(time.second))+'.txt'
path='../txt'
file_path=path + '/' + name
try:
with open(file_path, 'a+') as test_write:
test_write.writelines(self.get_value())#将列表数据写入至文件
except BaseException as e:
print(e)

def tousu(self):
self.url='http://'+api_host+'/ms-fahuobao-order/merchantComplain/complain'
with open(file_path,'r') as read_tousu:
for fhb_order_id in read_tousu.readlines(): #读取文件
# fhb_order_id=fhb_order_id.strip() #去除换行符,使用python自带的strip方法
fhb_order_id=re.sub('\W','',fhb_order_id) #使用正则表达式去除换行符,\W:匹配非字母数字及下划线
# print(fhb_order_id)
self.data={"fhbOrderId":fhb_order_id,"complainCauseCode":"CAUSE4","complainMemo":"小短裙","complainPicture":["5c1a04b5fe7ee20001ccbf32","5c1a04b8fe7ee20001ccbf36"]}
res=requests.post(self.url,json.dumps(self.data),headers=headers1)
print(json.dumps(self.data))
print(res.json())


if __name__=="__main__":
test1=test_write_value()
test1.get_value()
test1.write_value_to_txt()
test1.tousu()
posted @ 2018-12-19 17:17  wan了个蛋  阅读(1111)  评论(1编辑  收藏  举报