码迷,mamicode.com
首页 > 编程语言 > 详细

算是帮华仔写的撸JSON文件,然后发到我的REST接口的PYTHON代码

时间:2016-05-26 13:04:15      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

很久没有写过类似的代码了,感觉好陌生。。。

#!/usr/bin/python
#coding:utf-8

import json
import getopt
import sys
import 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 result

if __name__ == "__main__":
    main()

 

算是帮华仔写的撸JSON文件,然后发到我的REST接口的PYTHON代码

标签:

原文地址:http://www.cnblogs.com/aguncn/p/5530592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!