一丶接口测试平台之替换请求参数中的变量
1 # author: 倾丶几回断肠 2 3 import random 4 import string 5 import json 6 import re 7 8 9 def change_globe_val(obj): 10 """ 替换全局变量 """ 11 if isinstance(obj, dict): 12 obj = json.dumps(obj) 13 p = '\${(.*?)}' 14 keys = re.findall(p, obj) 15 values = [] 16 for i in keys: 17 # value = Val.objects.filter(name=i) 18 value = ''.join(random.choices(string.ascii_letters, k=6)) 19 values.append(value) 20 z = list(zip(keys, values)) 21 print(z) 22 newObj = obj 23 for k, v in z: 24 patt = '\${%s}' % k 25 newObj = re.sub(patt, v, newObj) 26 return newObj 27 28 29 d = '{"userName": "admin", "password": "${password}", "token": "${token}"}' 30 dd = {"user-agent": "${usreAgent}"} 31 new_obj = change_globe_val(d) 32 print(new_obj) 33 # new_obj = change_globe_val(dd) 34 # print(new_obj)
结果如下: