简体   繁体   English

Python - subprocess.Popen不返回输出

[英]Python - subprocess.Popen not returning output

I am using a Windows 7 machine right now have have STS installed with PyDev utility for python development. 我正在使用Windows 7机器,现在已经安装了使用PyDev实用程序进行python开发的STS。

My code wants to login to a machine and return the output. 我的代码想要登录到机器并返回输出。 My current code is: 我目前的代码是:

        ssh = subprocess.Popen(["uname -a"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        print (ssh)
        result = ssh.stdout.readlines()
        print (result)

This yileds an output of : 这赞成了一个输出:

<subprocess.Popen object at 0x02B35810>
[]

But the uname -a command should yield Linux 3.10.0-514.10.2.el7.x86_64 #1 SMP Mon Feb 20 02:37:52 EST 2017 x86_64 x86_64 x86_64 GNU/Linux 但是uname -a命令应该产生Linux 3.10.0-514.10.2.el7.x86_64 #1 SMP Mon Feb 20 02:37:52 EST 2017 x86_64 x86_64 x86_64 GNU/Linux

Any idea why I cant capture the output ? 知道为什么我不能捕获输出吗?

You can call uname with command/args properly split in a list like this: 您可以使用命令/ args调用uname ,在这样的列表中正确拆分:

subprocess.Popen(["uname","-a"] ...

or quick&dirty in a string like this (you have to handle quoting, but here there's none): 或像这样的字符串快速和脏(你必须处理引用,但这里没有):

subprocess.Popen("uname -a", ...

but not like this: 但不是这样的:

subprocess.Popen(["uname -a"] ...

else the actual issued command is "uname -a" (literally). 否则实际发出的命令是"uname -a" (字面意思)。 Of course, it doesn't exist, which should throw an exception, but since you're using shell=True it doesn't since it runs the nonexisting command in a shell (which exists, so subprocess doesn't complain) 当然,它不存在,应该抛出一个异常,但是因为你使用的是shell=True所以它不会因为它在shell中运行不存在的命令(存在,所以subprocess不会抱怨)

that's one of the numerous reasons why you shouldn't throw shell=True in without being absolutely forced to (for instance to run a shell internal command) 这是为什么你不应该在没有绝对强制的情况下抛出shell=True的众多原因之一(例如运行shell内部命令)

Checking the error stream (using result = ssh.stderr.readlines() ) would reveal something like "uname -a": command not found 检查错误流(使用result = ssh.stderr.readlines() )会显示类似"uname -a": command not found

Note that using PIPE for both output and error streams may lead to deadlock unless you're using ssh.communicate() to handle streams in separate threads internally. 请注意,对输出和错误流使用PIPE可能会导致死锁,除非您使用ssh.communicate()在内部处理单独线程中的流。

My suggestion for your problem would simply to use check_output as you don't need to run the command in background, you just need its output: 我对你的问题的建议只是使用check_output因为你不需要在后台运行命令,你只需要它的输出:

result = subprocess.check_output(["uname","-a"])

(which is slightly different because it doesn't create lines but a sole buffer, that you can easily apply strip / splitline onto) (略有不同,因为它不会创建线条而是唯一的缓冲区,您可以轻松地将strip / splitline到)

Note that if you're looking for a portable way to get system type or platform, I recommend you the os module ( os.name yields nt for windows for instance) and the platform module, where you can get system architecture, linux distribution, etc... 请注意,如果您正在寻找一种可移植的方式来获取系统类型或平台,我建议您使用os模块(例如, os.name为Windows生成nt )和platform模块,在这里您可以获得系统架构,Linux发行版,等等...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM