我想通過發送一系列命令並捕獲輸出來測試特定的聊天程序。在Python中一次讀取stdin行
如何傳遞:
sleep 2
echo test
sleep 2
echo test1
我已經試過這樣:
(sleep 2; echo test; sleep 2; echo test1) | python3 test.py
,但它只是打印第一部分,而第二個我什麼也沒得到。它進入了一個無限循環。
Python程序的代碼是:
import sys, select
while True:
socket_list = [sys.stdin]
read_sockets, write_sockets, error_sockets = select.select(socket_list, [], [])
for sock in read_sockets:
message = sys.stdin.readline()
sys.stdout.write("> %s: ")
sys.stdout.flush()
我應該提到這是不完整的程序,但它是它有助於重建完全相同的效果的部分。
瞎猜,但什麼'{睡眠2;回聲測試;睡2;回聲test1}'也許重定向到python而不是管道? –
'date +%s; (睡眠2;回聲測試;睡眠2;回聲測試1)| perl -pe'$ _ = time()。「」。$ _''同時顯示'test'字符串和兩秒延遲。 – thrig
你需要告訴我們你是如何發送和接收。這聽起來像是一個問題,但很難說。 –