zoukankan      html  css  js  c++  java
  • 简单实现将接口返回的数据写入文本,从文本读取参数,实现接口参数自动化

    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']+' ') #将数据添加至列表时追加换行符
    list_order_id.append(list_str['data'][order_no]['id'] + ' ') # 将数据添加至列表时追加换行符
    # 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()
    脑子不够用当然只能脚踏实地的做事情!
  • 相关阅读:
    leetcode 300. 最长上升子序列
    JAVA基础系列:Arrays.binarySearch二分查找
    leetcode 674. 最长连续递增序列
    小红书:笔试题(棋盘最短路径,笔记本草稿栈,迷宫游戏)
    VIPKID:笔试题(数组中和为0的一对数的数量,十进制转二进制中1的个数)
    [******] 树问题:普通二叉树的创建与遍历
    [******] 链表问题:将单向链表按某值划分成左边小、中间相等、右边大的形式
    [******] java多线程连续打印abc
    快手:笔试题(版本号比较,平方和为1,合并两个流)
    京东:笔试题(合唱队找剩余的最小值,考场安排搬出的人数尽可能少)
  • 原文地址:https://www.cnblogs.com/qtclm/p/10144563.html
Copyright © 2011-2022 走看看