我需要直接与我用stdin
产生的过程的stdout
和subprocess
进行交互。我可以这样做:
proc = subprocess.Popen("/bin/bash", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
proc.stdin.write(b"whoami\n")
print(proc.stdout.readline())
但是,只要进程分叉,我就无法再与之交互。我有这个测试程序:
// child.c
#include <unistd.h>
int main() {
execve("/bin/bash", NULL, NULL);
}
我编译的:
gcc -o child child.c
当我在child
调用中使用/bin/bash
而不是Popen
尝试上面的python代码时,我得到一个broken pipe
错误。
我尝试了什么:
由于python创建的文件描述符在默认情况下是不可继承的,所以我尝试改变它:
inr, inw = os.pipe()
outr, outw = os.pipe()
os.set_inheritable(inr, True)
os.set_inheritable(inw, True)
os.set_inheritable(outr, True)
os.set_inheritable(outw, True)
proc = subprocess.Popen("./child", stdin=inr, stdout=outw, stderr=outw)
proc.stdin.write(b"whoami\n")
print(proc.stdout.readline())
os.close(inr)
os.close(inw)
os.close(outr)
os.close(outw)
但我仍然得到同样的错误。我认为这应该是一件容易的事,我可能会遗漏一些东西。任何帮助表示赞赏。
[编辑]在我编辑我的帖子之前,我使用test
而不是child
作为我的测试c
可执行文件的名称。我了解到它确实是我的PATH中的一个程序,位于/usr/bin/test
。
将可执行文件名更改为child
成功解决了我的问题。经过数小时的试验和错误,我知道答案会变得简单......
非常感谢Barmar指出这一点!
当你打电话给test
时,你打电话给/bin/test
,而不是你的程序。
重命名您的C程序,使其不与标准命令冲突,或使用路径名访问它,因此它不会搜索$PATH
。
proc = subprocess.Popen("./test", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)