0
以下時,執行命令我寫的代碼來讀取SSH輸出示例代碼有時蟒蛇select.select給空字符串
stdin, stdout, stderr = ssh.exec_command(cmd)
channel = stdout.channel
stdin.close()
channel.shutdown_write()
stdout_chunks=[]
exit_status = 1
#stdout_chunks.append(stdout.channel.recv(len(stdout.channel.in_buffer)))
stdout_chunks.append(stdout.channel.recv(1024))
#or channel.recv_ready() or channel.recv_stderr_ready()
while not channel.closed or channel.recv_ready() or channel.recv_stderr_ready():
readq, _, _ = select.select([stdout.channel], [], [], timeout)
for c in readq:
if c.recv_ready():
stdout_chunks.append(stdout.channel.recv(512))
got_chunk = True
print stdout_chunks[-1]
if c.recv_stderr_ready():
print stderr.channel.recv_stderr(512)
got_chunk = True
if not got_chunk and stdout.channel.exit_status_ready() and not stderr.channel.recv_stderr_ready() and not stdout.channel.recv_ready():
stdout.channel.shutdown_read()
stdout.channel.close()
stdout.channel.recv_exit_status()
break
但是,當我readq在打印有時我得到空字符串像[]
因爲如此我的執行不會進一步進行。如何處理這種情況
選擇總是返回一個文件是否已經增長,因爲它是最後一次讀取,那麼它是如何返回空列表? –