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