Popen.stdout.readline()在Python 3.2中有效,但在Python 3.6中返回空字符串

Bel*_*los 5 python python-3.2 python-3.6

我正在使用Popen通过ssh启动telnet进程,通过telnet发送命令,并检查进程的输出以监视telnet的状态。我遇到的奇怪的事情是代码在Python 3.2.3上运行正常,但是当我在Python 3.6.5中运行且没有代码更改时,它无法获取输出。

我试过了

  • 冲洗标准输出

  • 在stdout.write之后等待长达10秒

  • 检查的stderr

def nonblockingread(sout):
   fd = output.fileno()
   fl = fcntl.fcntl(fd, fcntl.F_GETFL)
   try:
       return sout.read()
   except:
       return ""

process = Popen(shlex.split("ssh anon@server \"telnet 0 2323\""), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(nonblockingread(process.stdout)) # works for both versions
process.stdin.write(b"start service 4\n")
print(nonblockingread(process.stdout)) # works in 3.2 but not 3.6 (prints empty line)
print(process.stdout.readline()) # also prints empty line in 3.6
Run Code Online (Sandbox Code Playgroud)

Dav*_*ing 2

缓冲在3.2.4 和 3.3.1中默认打开,在 Python 3 中无意中关闭。您需要flushwrite另一方process.stdin看到任何内容。