blob: 04c1708f6d8e7f947e6c23d4502061b48171a926 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import subprocess
import json
class CommandException(Exception):
pass
def tsg_get_device_list_info_by_sn(sn):
command = "consul kv get device_list/%s" %(sn)
exitcode, output = subprocess.getstatusoutput(command)
#print("%d" %(exitcode))
if exitcode != 0:
raise CommandException(output)
return ""
else:
return output
def tsg_get_tags_by_sn(sn):
dev_list_info = tsg_get_device_list_info_by_sn(sn)
if len(dev_list_info) <= 0:
#写个空json
return "{}"
json_dict = json.loads(dev_list_info)
tags = json_dict['tags']
#重新组织成合法的json格式
dict_to_json = json.dumps(tags)
json_tags = "{\"tags\":%s}" %(dict_to_json)
return json_tags
if __name__ == '__main__':
print(tsg_get_tags_by_sn("CBT2201925000001"))
|