我使用以下代码通过ssh在远程Windows服务器上运行长时间运行的命令,并尝试获取sys.stdout
并将其实时写入文件。下面的代码打印sys.stdout但不将其写入文件。我做错了什么?
import wx
import wx.stc as stc
from subprocess import Popen, PIPE, STDOUT
import sys
import spur
import subprocess
import threading
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(600, 500), style=wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER |
wx.RESIZE_BOX |
wx.MAXIMIZE_BOX))
self.CreateStatusBar()
menuBar = wx.MenuBar()
menu = wx.Menu()
self.SetMenuBar(menuBar)
self.sshLog1 = sshLog(self)
self.Centre()
self.Show()
class sshLog(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.parent = parent
self.frame = self
self.running_log = wx.TextCtrl(self, pos=(5, 5), size=(570,390))
self.buttonGo = wx.Button(self, -1, "Go", pos=(180,420))
self.buttonGo.Bind(wx.EVT_BUTTON, self.Go)
self.buttonClose = wx.Button(self, -1, "Quit", pos=(285,420))
self.buttonClose.Bind(wx.EVT_BUTTON, self.OnClose)
self.Show()
def Go(self, event):
threading.Thread(target=self.runCommand).start()
def runCommand(self):
sys.stdout = open('file.txt', 'w')
shell = spur.SshShell(hostname='10.1.1.100', username='remoteUsername', password='remotePassword', missing_host_key=spur.ssh.MissingHostKey.accept)
result = shell.spawn(['ping', 'google.com', '-t'], stdout=sys.stdout)
def OnClose(self, e):
CloseApp()
###############################
########## CLOSE APP ##########
###############################
class CloseApp(wx.Frame):
def __init__(e):
sys.exit(0)
app = wx.App()
MainWindow(None, -1, 'My App')
app.MainLoop()