c - 如何将EOF发送到python中的进程

  • 本文关键字:python 进程 EOF c eof
  • 更新时间 :
  • 英文 :


现在我有一个像下面的程序:

#include <stdio.h>
#include <unistd.h>
int main(){
    char str[200];
    while(read(0, str,10)>0){
        printf("%s", str);
    }   
    printf("goodn");
    scanf("%s", str);
    printf("%sn", str);
}

收到EOF时,该程序将打印good然后继续接受我的下一个输入,然后在直接运行的情况下将其打印出来。

我要做的是与另一个python代码进行交互:

from pwn import *
r = process('./test')
r.send('123')
print r.recv(1000)
r.send('x04')
print r.recv(1000)

但无论如何,除非我关闭连接,否则我无法退出 while 循环。但是如果我关闭连接,我将无法发送最后一个输入。我在某处发现"\x04"代表 EOF。但它似乎不起作用。

想知道我应该怎么做才能退出循环,然后发送最后一个输入。

看起来您刚刚有了 stdin 和 stdout 流来与您的测试过程进行通信。 这还不足以发送 EOF,您需要一个 PTY。 你可以为此使用 ptyprocess:

from ptyprocess import PtyProcessUnicode
p = PtyProcessUnicode.spawn(['./test'])
p.write("123n")
print(p.read())
p.sendeof()
print(p.read())

最新更新