0
为什么“subprocess.Popen.stdin.write”命令看起来失败?写入subprocess.Popen.stdin不能正确执行
#!/usr/bin/env python3
# coding=utf-8
import os
import subprocess
bash = subprocess.Popen(['bash'],
stdout=subprocess.PIPE, stdin=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
(壳=真:见编辑2)
bash.stdin.write(b'echo foo\n')
print(bash.stdout.readline())
bash.stdin.write(b'echo bar\n')
print(bash.stdout.readline())
编辑:它会阻止在第一subprocess.Popen.stdout.readline(),可能是因为在subprocess.Popen没有线.stdout。它应该打印出来:
foo
bar
编辑2:这仍然挂起。
您是否收到任何错误讯息?它做什么和它应该做什么? – Adam
它不会这样做。 bash有许多执行模式,你要找的是shell模式。你可以做的是在调用'Popen'时执行'shell = True',但是你会创建一个安全漏洞。你应该检查文档:https://docs.python.org/2/library/subprocess.html – HuStmpHrrr
你会做什么bash?显然它看起来对我很有用,就像这样,因为当它在shell模式下启动时,你的bash会打印其中的一些内容。另一个解决方案是'ptty',它更正确但更复杂。如果你想做正确的事情,你也会想检查一下。 – HuStmpHrrr