脚本:
#coding=utf-8 import requests import os import yaml import json def yml(): #获取项目所在位置 path_dir = str(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) file = os.path.join(path_dir + 'python_test\api.yml') apidata = yaml.load(open(file)) return apidata def api_test(apidata): url = apidata["Login"]["Url"] data = apidata["Login"]["Data"] header = apidata["Login"]["Header"] a= requests.post(url = url, data = json.dumps(data),headers = header) print json.dumps(a.json(), indent=2, ensure_ascii=False) if __name__ == "__main__": a=yml() api_test(a)
yml文件:
Login: From: "POST" Url: "http://*******************/login" Header: { "Content-Type": "application/json" } Data: phone: "180********" password: "**********"