【发布时间】:2017-12-02 11:32:28
【问题描述】:
我已经编写了下面的 Expect 脚本,它可以工作。
我希望只有一根发送线和一根接收线。
(如果我在大括号内添加\n,它会在需要发送 0x10 时按字面意思发送。)
#!/usr/bin/expect
# N.B. The outer set of curly brackets are quotes in TCL/expect.
# The JSON received by the server only has a *single* set of outer curly
# brackets.
# The \n is sent on a separate line as the curly quote sends a literal \n,
# rather than a 0x10. (Double quotes *do* send a 0x10 for \n, but then I
# would have to escape every double quote inside the JSON.)
set timeout 30
spawn nc localhost 2222
send {{"method":"GET", "path":["ping"]}}
send "\n"
expect {{"code": 204}}
expect "\n"
如何在 Expect 中以更易读的方式发送和期待单行 JSON?
如何避免 sending 和 expecting 的 \ns 在自己的线路上?
【问题讨论】:
-
通过 Expect 控制的通道交换 JSON 似乎是一件非常奇怪的事情。 (您是否考虑过生成 JSON 而不是使用文字?)
-
@DonalFellows 我正在 (ab) 使用 expect 来测试 JSON 服务器...Expect 只是客户端的模拟。
-
即便如此,我仍然会在变量中真正构建对象字符串,然后执行
send "$json\n"或类似的操作将其移出。
标签: json tcl expect double-quotes curly-braces