diff options
Diffstat (limited to 'py_test/shell_cmd.py')
| -rw-r--r-- | py_test/shell_cmd.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/py_test/shell_cmd.py b/py_test/shell_cmd.py new file mode 100644 index 0000000..04c1708 --- /dev/null +++ b/py_test/shell_cmd.py @@ -0,0 +1,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")) |
