接口参数保存到文件的保存几种方式ini文件,json文件,yaml文件

interface.ini

# coding=uttf-8
[数据库测试接口]
host=http://www.baidu.com
header={key:value}
param={key:value}
method=get



pt2: json 保存


{
  "登陆接口":{
    "author":"JackChen",
    "bug":"我想吐槽",
    "cookie":{
      "dc_session_id":"10_1541690279946.617154"
    },
    "data":[
      {
        "body":"{\"re\":\"ref=https%3A%2F%2Fpassport.csdn.net%2Faccount%2Fverify&mtp=2&mod=ad_popu_282&con=ad_content_3743%2Cad_order_719&uid=chen498858336&ck=-\"}",
        "headers":{
          "component":"enterprise",
          "datatype":"re",
          "version":"v1"
        }
      }
    ],
    "header":{
      "Content-Type":"text/plain;charset=UTF-8"
    },
    "method":"post",
    "url":"https://pv.csdn.net/csdnbi"
  }
}


pt3: yaml 保存
data: [2, 3, 4, 5]
student:
  age: 20
  love: {ball: volleyball, book: Python}
  name: aa
teacher: {age: 20, name: bb}




"""写入yaml"""
with open('./dump.yaml','w') as f:
     d ={
        'student':{
            'name':'aa',
            'age':20,
            'love':{
                'ball':'volleyball',
                'book':'Python'
            }
        },
        'teacher':{
            'name': 'bb',
            'age': 20
        },
        'data':[2,3,4,5]
     }
     yaml.dump(d,f)

# """读取yaml内容"""
with open("./dump.yaml", 'r') as f:
    datas=yaml.load(f)


ini文件读取
# coding = utf-8

import os
import ConfigParser
config_dir = os.path.split(os.path.realpath(__file__))[0]
config_path = os.path.join(config_dir, "config.ini")


def get_config_value(section, option):
    config = ConfigParser.SafeConfigParser()
    config.read(config_path)
    value = config.get(section='config', option='ip')
    print type(value)
    print config.options("config")
    print config.items("mysqlconf")
    print config.sections()  # 名字


json写入读取

# coding=utf-8
import  json
import requests
data ={"登陆接口":
           {"method": "post",
                    "url": "https://pv.csdn.net/csdnbi",
                    "data": [{"headers":{"component":"enterprise","datatype":"re","version":"v1"},"body":"{\"re\":\"ref=https%3A%2F%2Fpassport.csdn.net%2Faccount%2Fverify&mtp=2&mod=ad_popu_282&con=ad_content_3743%2Cad_order_719&uid=chen498858336&ck=-\"}"}],
       "header": {"Content-Type": "text/plain;charset=UTF-8"},
        "cookie": {"dc_session_id": "10_1541690279946.617154"},
        "author": "JackChen"
    }
 }

class JsonTransfer(object):
    """3.x以上的json文件读取中文乱码用这个解决,3.x默认utf-8,dump你再加异常很bug"""
    def write_read_json(self):
        with open('./ReadJson.json', 'w')as f:
            json.dump(data,f,ensure_ascii=False,separators=(',',':'),sort_keys=True,indent=3)
        f= open('./ReadJson.json','r')
        dict =json.load(f)
        # print(dict)
        """如果有人还有问题请用以下输出,注意3.x默认encoding=utf-8,你再加就异常了dumps里面"""
        """------------------"""
        report_show = json.dumps(dict,ensure_ascii=False,indent=3,separators=(',', ':'))
        print(report_show)
        """--------------------"""

        cookie=dict['登陆接口']['cookie']
        js =dict['登陆接口']['data']

        url=dict['登陆接口']['url']
        header =dict['登陆接口']['header']
        rep =requests.post(url=url,headers=header,json=js,cookies=cookie)
        return rep.text  #在2.x这里是content也可以的3.x,我都不想吐槽了

if __name__ == "__main__":
   print(JsonTransfer().write_read_json())













 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值