我正在尝试在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"
]
这可能是最复杂的情况,因为通常参数是一个字符串,在这种情况下
r18.set
会被用来代替
r18.hmset
.
我认为在python中必须有一些内置模块,因为这就像解析命令行字符串一样。