我正在寻找一个python模块,它通过STDIO提供一个真正的交互式操作系统shell(在Linux、win和macos中)。我尝试过使用subprocess+PIPES和/或QProcess来启动csh/bash或命令行.exe然后通过qtexted keyPressEvents提供一个粗略的终端IO。对于基本的操作系统命令,这是可以工作的,但是如果我想在shell进程中启动另一个应用程序(例如telnet、ssh、tcl、python、netstat等),stdin和stdout将不再附加,这可能是因为正在创建一个新的子进程。我是否需要对每个命令进行subprocess/QProcess并跟踪所有子进程?--那会很难看,一定有更好的方法。。。 (我也看过pexpect/wexpect,但无法使其可靠地工作。)
环境:Win7、RHEL6.4、Python 2.7.8、Qt 4.8.6
请求的模块似乎是标准PyQt库的一部分;我是否遗漏了什么? 也许在python中不需要真正的交互式操作系统shell。在
简陋外壳终端示例片段:
class Console(QtGui.QTextEdit):
def __init__(self, startup_message='', parent=None):
super(Console, self).__init__(parent)
. . .
# TextEdit settings
self.setAcceptRichText(True)
#QProcess setup
self.proc = QtCore.QProcess()
self.proc.setProcessChannelMode(QtCore.QProcess.MergedChannels)
self.proc.setReadChannelMode(QtCore.QProcess.MergedChannels)
QtCore.QObject.connect(self.proc, QtCore.SIGNAL("readyReadStandardOutput()"), self.procReadStdOutput)
. . .
if sys.platform == "win32":
self.proc.start(r"cmd.exe", mode=QtCore.QProcess.ReadWrite)
elif sys.platform == "linux2":
self.proc.start(r"csh", mode=QtCore.QProcess.ReadWrite)
. . .
def runCommand(self):
if self.userTextEntry != "":
self.userCommand = self.userTextEntry
self.userTextEntry = ""
try:
self.proc.writeData(self.userCommand)
except Exception as error:
self.append(str(error))
else:
self.append(self.prompt)
return
. . .
def keyPressEvent(self, event):
if event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):
event.accept()
self.userTextEntry += "\n"
self.runCommand()
. . .
@QtCore.pyqtSlot()
def procReadStdOutput(self):
self.append(QtCore.QString(self.proc.readAllStandardOutput()))
self.append(self.getOSPrompt())
self.proc.emit(QtCore.SIGNAL("cmdDone()"))
return
目前没有回答
相关问题 更多 >
编程相关推荐