【问题标题】:Pop up message box after connecting to SSH连接SSH后弹出消息框
【发布时间】:2013-06-25 05:05:30
【问题描述】:

大家好,我正在学习 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()

【问题讨论】:

  • 弹出消息框的代码在哪里?
  • 不在这个.. 我只是想知道我们有什么方法可以显示消息并克服 GUI 冻结
  • 你不能只问这个就期待答案。这是你做错了。我们如何调试我们看不到的代码?!
  • 好的,抱歉。我将在这里添加我的实际代码。

标签: python ssh wxpython messagebox


【解决方案1】:

GUI 可能会冻结,因为 SSH 连接阻塞了主循环。要克服这个问题,您必须将连接代码放入单独的线程中。然后使用 wxPython 的线程安全方法之一(wx.CallAfter、wx.CallLater 或 wx.PostEvent)告诉 GUI 显示一个弹出对话框。

有关 wxPython 和线程的信息,请参阅以下链接:

【讨论】:

    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 2017-05-29
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多