简体   繁体   English

如何在不通信的情况下读取python子进程标准输出

[英]How to read python subprocess stdout without communicate

I have a python 3 script that accepts input from the user, this input is piped into a subprocess, where a shell has been spawned. 我有一个python 3脚本,可以接受来自用户的输入,此输入通过管道传递到子进程中,在该子进程中已经生成了shell。 Originally I was going to put this code together with a socket, to be able to make my own non-serious remote administration tool, however this proves too hard for my level currently. 最初,我打算将此代码与套接字放在一起,以便能够制作自己的非严肃的远程管理工具,但是,这对于我目前的水平来说太难了。 Code: 码:

import subprocess

p1 = subprocess.Popen(["/bin/sh"], stderr = subprocess.PIPE, stdin = subprocess.PIPE, stdout = subprocess.PIPE, encoding = "utf-8")

command = input("Command: ")

p1.stdin.write(command)

p1.stdout.read()

Problem: Nothing gets printed out. 问题:什么都没有打印出来。 I have searched endless hours online for a reason, over multiple days, but all of them don't seem to work, and/or advise using communicate() which is something I do not want to do. 我已经在网上搜索了无数小时,这是有原因的,已经过了好几天,但是所有这些似乎都不起作用,并且/或者建议使用communication(),这是我不想做的事情。 When thinking ahead if I am able to implement this with a socket, I can't have the process closing after each command. 在考虑是否可以使用套接字实现此功能时,无法在每个命令之后关闭进程。 I have also tried flushes everywhere, before write, inbetween the read, after the read, pretty much everywhere you can think of. 我还尝试过在所有位置(在写入之前,在读取之间,在读取之后)在所有您能想到的地方都进行刷新。 It should be simple enough, without me having to look deep into the io module or the rules of buffering (too late now). 它应该足够简单,而无需我深入研究io模块或缓冲规则(现在为时已晚)。 I have been struggling with this for days on end. 我已经为此苦苦挣扎了好几天了。

You have a couple of issues, I will try to draw the path, I hope I am not misleading you. 您有几个问题,我会尽力而为,希望我不会误导您。

First of all, disclaimer: executing user input is inherently unsafe. 首先,免责声明:执行用户输入本质上是不安全的。 If that's not an issue, let's keep on your problem. 如果这不是问题,让我们继续解决您的问题。

I would start by doing 我会先做

p1 = subprocess.Popen(["/bin/sh"], stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True, bufsize=1)

Having a line-buffered PIPE is a good idea, in this scenario. 在这种情况下,使用行缓冲的PIPE是一个好主意。

Then, remember that you have to press enter after the command, so: 然后,请记住您必须在命令后按Enter键,因此:

command = input("Command: ") + "\n"

The write is correct, 写的是正确的,

p1.stdin.write(command)

But the, the read is VERY dangerous and you will trivially become deadlocked. 但是,读操作非常危险,您将陷入僵局。 Because the stdout is open (without an EOF or anything like that) you should read conservatively, and still, you will have problems on that. 因为stdout是打开的(没有EOF或类似的东西),所以您应该保守地阅读,但仍然会有问题。

A first idea would be to read lines: 第一个想法是读取以下行:

p1.stdout.readline()

But, if you don't know how many lines you can read... then that's a problem. 但是,如果您不知道可以阅读多少行,那就是一个问题。 In fact, that specific problem has already been asked . 实际上, 已经提出了该特定问题。

If you want to experiment, just open an interactive python interpreter, send a "ls\\n" command and perform readline() . 如果您想尝试,只需打开一个交互式python解释器,发送"ls\\n"命令并执行readline() It will work, until you read the last line, and then... it will wait forever. 它会起作用,直到您读完最后一行,然后……它将永远等待。 That is bad behaviour. 这是不良行为。 So you will need to solve that. 因此,您需要解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python子进程Popen.communicate()相当于Popen.stdout.read()? - Python subprocess Popen.communicate() equivalent to Popen.stdout.read()? Python:如何以非阻塞方式读取子进程的标准输出 - Python: How to read stdout of subprocess in a nonblocking way 如何在 Python 子进程中立即读取标准输出 - How to read stdout immediately in Python subprocess Python子进程stdout不读取 - Python subprocess stdout does not read python subprocess在执行时读取stdout - python subprocess read stdout as it executes Python子进程交互,为什么我的进程与Popen.communicate一起工作,而不是Popen.stdout.read()? - Python subprocess interaction, why does my process work with Popen.communicate, but not Popen.stdout.read()? 如何读取子进程的stdout的第一个字节,然后在Python中丢弃其余的? - How to read the first byte of a subprocess's stdout and then discard the rest in Python? 如何在python子进程命令中将stdout输出到文件而没有任何缓冲? - How to stdout in python subprocess command to a file without any buffering? Python Subprocess stdout 和communication()[0] 不打印终端会的输出 - Python Subprocess stdout and communicate()[0] not printing output that terminal would 如何清除Python子进程中的stdout? - How to clear stdout in Python subprocess?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM