Haven't written similar code for a long time, feel good strange ...
#!/usr/bin/python#Coding:utf-8ImportJSONImportgetoptImportSYSImportRequests#get command line argumentsdefget_opt ():Try: Config_opt= {"deploy_version":"None","file":"Jsonreport.json"} opts, args= Getopt.getopt (sys.argv[1:],"hv:f:", [" Help"]) foroption, Valueinchopts:ifOptioninch["- H","--help"]: Print """usage:%s-v deploy_version-f File""" elifOptioninch['- v']: config_opt["deploy_version"] =valueelifOptioninch['- F']: config_opt["file"] =valuereturnconfig_optexceptgetopt. Getopterror:Print """usage:%s-v deploy_version-f File"""sys.exit ()#Loading JSON filesdefLoad_json (file_name): Json_str=json.load (Open (file_name))returnjson_str[" Total"], json_str["fail"]#send a POST request to the rest interfacedefrest_post (payload): Rest_api_url="http://10.2.4.3/api/" PrintRest_api_url, PayloadreturnRequests.post (Rest_api_url, Data=payload)#Main program Execution FlowdefMain (): 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)Printresultif __name__=="__main__": Main ()
The
is a Andy json file to write to and then send to the Python code of my Rest interface