工作中,发现多个提供者被禁用,fsp个数多个62个,手动一个个启动,实在浪费时间,因此,脚本解决该痛点
功能:
1、支持多个提供者启动
2、支持无fsp提供者提示
脚本如下:
1 import base64 2 import json 3 import requests 4 5 def get_fsp(name, count): 6 r = { 7 "uid": "", 8 "token": "", 9 "nickname": "", 10 "r": 0.577604849553434, 11 "name": name, 12 "start": 0, 13 "limit": count, 14 "sortname": "", 15 "sortorder": "" 16 } 17 r = json.dumps(r) 18 # base64加密 19 bre = str(base64.b64encode(r.encode("utf-8")), "utf-8") 20 url = "http://xxx/fsp/register/service/query?" 21 total_url = url + bre 22 res = requests.get(total_url) 23 bbs = str(base64.b64decode(res.text), "utf-8") 24 bs = json.loads(bbs) 25 fsp_rows = bs['data']['rows'] 26 if len(fsp_rows) == 0: 27 print("该关键词下没有相关fsp,请输入正确的搜索关键词") 28 return fsp_rows 29 30 def get_fsp_count(name): 31 r = { 32 "uid": "", 33 "token": "", 34 "nickname": "", 35 "r": 0.577604849553434, 36 "name": name, 37 "start": 0, 38 "limit": 162, 39 "sortname": "", 40 "sortorder": "" 41 } 42 r = json.dumps(r) 43 # base64加密 44 bre = str(base64.b64encode(r.encode("utf-8")), "utf-8") 45 url = "http://xxx/fsp/register/service/query?" 46 total_url = url + bre 47 res = requests.get(total_url) 48 bbs = str(base64.b64decode(res.text), "utf-8") 49 bs = json.loads(bbs) 50 count = bs['data']['count'] 51 return count 52 53 54 def get_fsp_name(namelist): 55 fsp_name_list = [] 56 for i in namelist: 57 fsp_name_list.append(i['name']) 58 return fsp_name_list 59 60 def update_config(name, providerAddress): 61 r = { 62 "uid": "xxx", 63 "token": "", 64 "nickname": "xxx", 65 "r": 0.050240422960323716, 66 "name": name, 67 "providerAddress": providerAddress, 68 "forbidOper": 1 69 } 70 r = json.dumps(r) 71 # base64加密 72 bre = str(base64.b64encode(r.encode("utf-8")), "utf-8") 73 url = "http://xxx/fsp/register/provider/config" 74 total_url = url 75 res = requests.post(total_url, data=bre) 76 bbs = str(base64.b64decode(res.text), "utf-8") 77 json_data = json.loads(bbs) 78 if json_data['errorCode'] == 590000: 79 print(name + '更新成功') 80 return True 81 82 83 def check_ableFlag(name): 84 r = { 85 "uid": "201030", 86 "token": "", 87 "nickname": "", 88 "r": 0.7631927389548474, 89 "name": name, 90 "start": 0, 91 "limit": 10, 92 "sortname": "", 93 "sortorder": "" 94 } 95 r = json.dumps(r) 96 # base64加密 97 bre = str(base64.b64encode(r.encode("utf-8")), "utf-8") 98 url = "http://xxx/fsp/register/service/address/query?" 99 total_url = url + bre 100 res = requests.get(total_url, data=bre) 101 bbs = str(base64.b64decode(res.text), "utf-8") 102 bs = json.loads(bbs) 103 # 提供者个数 104 provider_num = len(bs['data']['rows']) 105 provider_af_pa =[] 106 if provider_num == 0: 107 print(name + '该fsp没有提供者') 108 else: 109 for i in range(provider_num): 110 ableFlag = bs['data']['rows'][i]['ableFlag'] 111 providerAddress = bs['data']['rows'][i]['providerAddress'] 112 r = dict(ableFlag=ableFlag, providerAddress=providerAddress) 113 provider_af_pa.append(r) 114 return provider_af_pa 115 116 def exe(key): 117 # 获取fsp个数 118 count = get_fsp_count(key) 119 # 获取fsp列表 120 name = get_fsp(key, count) 121 # fsp name列表 122 name_list = get_fsp_name(name) 123 for name in name_list: 124 # <class 'dict'>: {'ableFlag': 0, 'providerAddress': 'xxx:10310'} 125 pv_list = check_ableFlag(name) 126 for j in range(len(pv_list)): 127 ableFlag = pv_list[j]['ableFlag'] 128 pa = pv_list[j]['providerAddress'] 129 # labelflag==1是被禁用了,想启用的话"forbidOper": 1 130 if ableFlag == 1: 131 res = update_config(name, pa) 132 if res: 133 print(name + ' 已被启用') 134 else: 135 print(name + ' 该fsp已启动') 136 137 138 if __name__ == '__main__': 139 import sys 140 if len(sys.argv) != 2: 141 print("usage: python " + sys.argv[0] + " fsp.py xxx") 142 else: 143 exe(sys.argv[1])