1. 程式人生 > >簡單實現將介面返回的資料寫入文字,從文字讀取引數,實現介面引數自動化

簡單實現將介面返回的資料寫入文字,從文字讀取引數,實現介面引數自動化

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()