def request_argv(args, pre_api=None): ''' 命令行参数解析成dict类型,并执行。参数里可以有$a/b/c 类型变量 :param args: cmd参数组成的dict :return: 执行的结果 ''' param_dic = {} if '-p' in args.keys(): v = args['-p'] if re.match("\{('\w+'\s*:\s*'?\$?\w+'?)(?:\s*,\s*'\w+':\s*'?\$?\w+)*'?\}", v): ''' 预先加载的接口中,有$变量引用的,先替换变量,被应用的变量,需是常量,或者在引用之前被计算 ''' reg = '(\$(?:\*|[\w/]+))' var_list = re.findall(reg, v) # getStudentInfo.do -p {'studentId':$studentId} for va in var_list: rand = utils.random_select(utils.find_by_path(pre_api, va[1:])) v.replace(va, str(rand)) param_dic['-p'] = v # -p {userId:1, loginId:123@qq, ...} 转化成dict if re.match("\{('\w+':\s*'?[\w@]+'?)(?:\s*,\s*'\w+':\s*'?[\w@]+'?)*}", v): param_dic = eval(v) # ready to request cfg = Apis() api_dic = cfg.find_api(args['name']) api_dic['params'].update(param_dic) newp = Api(api_dic, True if '-v' in args.keys() else False) return newp.request_and_find(args['-j'] if '-j' in args.keys() else None)
class Tasks(object): ''' 加载任务参数配置文件类 ''' def __init__(self, path=task_path): f = open(path, 'r', encoding='utf-8') self.tasks = json.load(f) self.apis = Apis() f.close() def find(self, task): return self.tasks[task] if task in self.tasks else None def task_list(self): tasks = [] for t in self.tasks: api = self.apis.find_api(t['name']) task = Task(t, api) tasks.append(task) return tasks
def __init__(self, path=task_path): f = open(path, 'r', encoding='utf-8') self.tasks = json.load(f) self.apis = Apis() f.close()
''' try: jdic = json.loads(jsn) find_v = utils.find_by_path(jdic, jpath) return find_v if find_v is not None else None except: print('json 格式错误') return None def printv(self, *args): ''' 打印,只有在-v时有效。 -v命令下,如果args只有一个值,输出该值,否则,第一个值为格式化字符串,后面的值为对应值 :param args: :return: ''' if self.verbose: if len(args) > 1: print(args[0] % args[1:]) elif len(args) == 1: print(args[0]) if __name__ == '__main__': argv = Argv.parse_argv(sys.argv[1:]) name = argv['name'] cfg = Apis() api = cfg.find_api(name) p = Api(api) print(p.request())
import requests import telnetlib from threading import Lock, Thread from apis import Apis from random import choice import redis import time from qq_hook import Hook from settings import Ip_db_settings c = Ip_db_settings() apis = Apis() #读setting send_to_qq = c.send_to_qq t_num = c.t_num tel_timeout = c.tel_timeout apis.timeout = c.api_timeout # pool = redis.ConnectionPool(host='127.0.0.1', port=6379, max_connections=200) num = 0 l = Lock() def cut(text, num): #将列表text分为num份 if len(text) < num: print('\033[1;33m[' + str(num) + ' is too big.]\033[0m') if len(text) % num != 0: t = len(text) // num + 1 else: t = len(text) / num t = int(t)
import sys import pika import json from apis import Apis channel = None connection = None apis = Apis() connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() channel.queue_delete(queue='request') channel.queue_delete(queue='response') channel.queue_declare(queue='request') channel.queue_declare(queue='response') def executor(ch, method, properties, body): print("request received : " + body.decode('utf-8')) print(properties) request = json.loads(body) method = request["method"] params = request["params"] reply_to = request["replyTo"] correlationId = request["correlationId"] returnVal = apis.execute(method, params) print("Sending response : " + str(returnVal)) # # send a message back