如何获得终端输出?

时间:2013-04-01 00:09:32

标签: python subprocess raspberry-pi

背景

作为一个有趣的项目,我决定为我的新Raspberry Pi创建一个远程控制应用程序。我知道如何使用SSH,但我想在Python中实现类似的东西。如何将打印到终端上的文本显示在我的Python shell中?

我检查了this SO question,但它没有用。由于没有工作,我的意思是我无法看到输出文本。如果我输入passwd,它只给我

Changing password for User.

但它没有给我交互式会话。 那是是我想要弄清楚的。

注意:我的Raspberry Pi运行在“Wheezy”上。此外,如果此程序可以与其他操作系统(例如Windows 7,Mac OS X)一起使用,那将会很棒。

代码

import subprocess
import socket
import sys
import platform
import os

print "WELCOME TO xTROL. \n"

pyVER = sys.version
OS = platform.system()

print pyVER, "\n"
print "Running on %s" % (OS)
while True:
    INPUT = raw_input(">>>")
    cmd_FORMAT = INPUT.split()

    try:
        output = subprocess.Popen(cmd_FORMAT, stdout=subprocess.PIPE).communicate()[0]
        print output
    except Exception:
        print "Something went wrong."

1 个答案:

答案 0 :(得分:0)

如果你想写一些类似SSH的东西,你必须制作一个伪终端。请看the pty module