#!/usr/bin/python
import requests
import json
import pymysql

def importdemo():
    #配置数据库连接
    db = pymysql.connect("你的服务地址", "你的用户名", "你的密码", "你的数据库")
    cursor = db.cursor()
    sql = "INSERT INTO testcaseinfo(create_time,update_time,type,name,belong_project,include,author,request,belong_module_id) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s')"

    #获取文档内容
    page = requests.get("http://你的域名/activityCenter/v2/api-docs")
    pagevalue  = page.content.decode("utf-8")
    dict1 = json.loads(pagevalue)

    print("dist1的类型:"+str(type(dict1)))
    #获取出paths中的
    keys = list(dict1['paths'].keys())
    print(keys)

    #所有的url键和值
    path = dict1.get('paths')
    print("path的类型:"+str(type(path)))
    baseurl = "http://你的域名"
    message = "message"
    #取出每个url和对应的值
    for key in keys:
        param = []
        data = {}
        request = {}
        test = {}
        interface = {}
        url = ''
        summary = ''#接口名称
        body = path[str(key)]
        if(list(body.keys())[0]=="post"):
            summary = body['post']['summary']
            parametersbody = body['post']['parameters']
            for v in parametersbody:
                value =v['name']
                param.append(value)
                data[value] = ''
                print("------------")
                print(str(v['name']))
                print(type(v['name']))
                # print(str(param))
                print("------------")
        url = baseurl + "/" +message + key
        if(list(body.keys())[0]=="post"):
            method = "POST"
        else:
            method = "GET"
        #封装request
        request['url'] = url
        request['method'] = method
        request['data'] = data
        request['headers'] = ''
        #封装test
        test['request'] = request
        #封装最外层
        interface['test'] = test
        interface['name'] = summary
        #将字典转化成字符串
        interfacestr = str(interface)
        #将每个分号前加上转义符
        cc = pymysql.escape_string(interfacestr)
        cursor.execute(sql % ('2019-04-24 11:00:00', '2019-04-24 11:00:00', '1', str(summary), '兼职', '[]', 'chenchi', cc , '1'))
        db.commit()
        print("------------")
        print(str(param))
        print(type(param))
        print("data:"+str(data))
        print("url:"+ url)
        print("requset:"+str(request))
        print("test:"+str(test))
        print("interface:"+str(interface))
        print("interface:" + str(type(interface)))
        print("------------")


if __name__ == '__main__':
    importdemo()
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐