大家好,我正在学习 Python 并使用 wxPython 制作 GUI 模型。
我想连接vai SSH,为此我使用了pexpect。我想显示一个消息框,说“已连接到服务器”,或者如果断开连接,“连接未建立”我不知道如何执行此操作,并且 GUI 在连接时冻结。如何避免冻结 GUI?我的示例代码是:
import time
import sys
import pexpect
c = pexpect.spawn("ssh -Y -L xxxx:localhost:xxxx user @ host.com")
#time.sleep(0.1)
c.expect("[pP]aasword")
c.sendline("xxxxxx")
#time.sleep(0.2)
c.interact()
c.pexpect([user@host.com~]$)
在此处连接到 SSH 后,GUI 冻结。连接后,我想在消息框中显示连接状态,而不是在终端中。请建议如何做;作为初学者,我觉得很难。
提前致谢。
更新:
import wx
import os
import pexpect
import sys
import subprocess
import time
class Connect_ssh(wx.Frame):
def __init__ (self, *args, **kw):
wx.Frame.__init__(self,None,wx.ID_ANY,"Secure Shell", size=(310,200),style=wx.DEFAULT_FRAME_STYLE ^ wx.MAXIMIZE_BOX ^ wx.RESIZE_BORDER)
panel = wx.Panel(self)
txt1 = wx.StaticText(panel, label="Account name:",pos=(20, 55))
txt2 = wx.StaticText(panel, label="Password",pos=(20, 105))
self.txt_name = wx.TextCtrl(panel, -1, size=(130, -1), pos=(160,50))
self.txt_pswd= wx.TextCtrl(panel, -1, size=(130, -1),pos= (160,100),style=wx.TE_PASSWORD)
button1 = wx.Button(panel, -1, "Connect",size=(-1,-1), pos=(50, 160))
button2 = wx.Button(panel, -1, "Disconnect",size=(-1,-1), pos=(170, 160))
self.Bind(wx.EVT_BUTTON,self.OnConc,button1)
def OnConc(self,event):
u_name = self.txt_name.GetValue()
passwd = self.txt_pswd.GetValue()
child = pexpect.spawn("ssh -Y -L xxx:localhost:xxx %s@host.com" % (str(u_name)))
child.expect("%s@host.com's password:" % (str(u_name)) )
child.sendline("%s" % (str(passwd)))
child.interact()
#child.sendline("%s" % str(sub))
child.expect("[%s@qvislabs.com~]$"% (str(u_name)) )
#time.sleep()
#self.Destroy()
msg = wx.MessageBOx(" '%s'@host.com is connected" % (str(u_name)), "Info",wx_OK)
self.Hide()
if __name__=="__main__":
app = wx.App()
Connect_ssh().Show()
app.MainLoop()