1
我知道这已被回答了很多次,但我还没有找到一种工作方式来做到这一点在我的情况下......我希望你会帮助。 我想要从stdout和/或stderr实时地将Popen调用的数据输出到套接字连接,而不是标准输出。所以sys.stdout.flush()不适合我。 顺序发送标准输出从subprocess.Popen通过套接字
data=sok.recv(512) #the command to execute
p = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE stderr=subprocess.PIPE)
#this work.. but not how i spected, if the subprocess ends too fast, i only get the first line
while p.poll() is None:
sok.send(p.stdout.readline())
#this only send the last line
for i in p.stdout.readline():
sok.send(i)
sys.stdout.flush() #why sys.stdout? i don't use it
p.stdout.flush() #same result
你打电话'Popen'用'壳= TRUE'数据你从网络连接中获得?你知道你是用这种方式创建一个shell服务器,不是吗? –
是的,我知道,但那不是重点。事实上,这正是我想要做的,为了好玩。 –