很久没有写过类似的代码了,感觉好陌生。。。
#!/usr/bin/python#coding:utf-8import jsonimport getoptimport sysimport requests#获取命令行参数def get_opt(): try: config_opt = { "deploy_version": "None", "file": "jsonReport.json"} opts, args = getopt.getopt(sys.argv[1:], "hv:f:", ["help"]) for option, value in opts: if option in ["-h", "--help"]: print """usage:%s -v deploy_version -f file""" elif option in ['-v']: config_opt["deploy_version"] = value elif option in ['-f']: config_opt["file"] = value return config_opt except getopt.GetoptError: print """usage:%s -v deploy_version -f file""" sys.exit()#载入JSON文件def load_json(file_name): json_str = json.load(open(file_name)) return json_str["total"], json_str["fail"]#发送POST请求给REST接口def rest_post(payload): rest_api_url = "http://10.2.4.3/api/" print rest_api_url, payload return requests.post(rest_api_url, data=payload)#主要程序执行流def main(): config_opt = get_opt() deploy_version = config_opt["deploy_version"] total, fail = load_json(config_opt["file"]) payload = { 'deploy_version': deploy_version, 'total': total, 'fail': fail} result = rest_post(payload) print resultif __name__ == "__main__": main()