我正在尝试在 Redis 服务器和 MQTT 之间建立一座桥梁,以便在更新 Redis 数据库时,这些更新通过 MQTT 分派给客户端。为此,客户端(只有一个,桥接器)连接到 Redis 数据库并开始监视它。我的问题是解析命令,更具体地说是其中包含的参数,它是一个以空格分隔的字符串列表。例如,当我将以下哈希存储在 Redis 中时data = { "key-3-1-json": "value-1", "key-3-2-json": 'this "this is \'quoted\' text"',}print r18.hmset("test-hash", { "key-1": "value-1", "key-2": 'this "this is \'quoted\' text"', "key-3": json.dumps(data),})客户收到以下信息1549578825.1 0 HMSET test-hash "key-3" "{\"key-3-1-json\": \"value-1\", \"key-3-2-json\": \"this \\\"this is 'quoted' text\\\"\"}" "key-2" "this \"this is 'quoted' text\"" "key-1" "value-1"如您所见,我已经在解析时间戳、数据库 ID、命令和密钥,但最后一部分,我不知道如何从中创建字符串列表。此消息随后将通过 MQTT 发送为mqtt.publish("redis/mon/0/HMSET/test-hash", json.dumps(args))args会在哪里[ "key-3", "{\"key-3-1-json\": \"value-1\", \"key-3-2-json\": \"this \\\"this is 'quoted' text\\\"\"}", "key-2", "this \"this is 'quoted' text\"", "key-1", "value-1"]这可能是最复杂的情况,因为在使用 wherer18.set代替r18.hmset.我认为 Python 中必须有一些内置模块可以做到这一点,因为它就像解析命令行字符串一样。
1 回答

犯罪嫌疑人X
TA贡献2080条经验 获得超4个赞
该subprocess
模块的文档说明subprocess.Popen()
使用shlex.split()
( shlex: Simple lexical analysis )
调用shlex.split(args_str)
有效地将参数字符串转换为所需的子字符串列表。
添加回答
举报
0/150
提交
取消