我如何调用(例如)一个函数连接到一个函数来使用wxPython进行搜索

时间:2011-09-09 20:24:46

标签: wxpython

以下是我的程序使用Paramiko连接到Ubuntu服务器的五个函数。我在Python中编写了一个命令行脚本来完美地处理这个问题,但我正在尝试学习wxPython并且我遇到了一些挑战。如果我有一个函数,脚本工作正常,但是,作为一个新手,我正在尝试编写更高效的代码。正如函数所示,我收到一条消息“ssh未定义......”我试过传递参数,以及其他组合......我想我忽略了一些东西。有人可以帮助我吗?

def OnIP(self, event):
    panel=wx.Panel(self)
    dlg = wx.TextEntryDialog(None, "Enter the IP Address.",
    'Dispenser Connect', 'xxx.xxx.xxx.xxx')
    if dlg.ShowModal() == wx.ID_OK:
        ip_address = dlg.GetValue()
    if ip_address:    
        cmsg = wx.MessageDialog(None, 'Do you want to connect to: ' + ip_address,
                                'Connect', wx.YES_NO | wx.ICON_QUESTION)
        result = cmsg.ShowModal()

    if result == wx.ID_YES:
        self.DispConnect(ip_address)

        cmsg.Destroy()
    dlg.Destroy()

    return True


def GoodConnect(self):
    gdcnt = wx.MessageDialog(None, 'You are connected!', 'ConnectionStatus', wx.ICON_INFORMATION)
    gdcnt.ShowModal()
    if gdcnt.ShowModal() == wx.ID_OK:
        self.OnSearch()
    gdcnt.Destroy()    


def ErrMsg(self):
    ermsg = wx.MessageDialog(None, 'Invalid Entry!', 'ConnectionDialog', wx.ICON_ERROR)
    ermsg.ShowModal()
    ermsg.Destroy()



def DispConnect(self, address):
    pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
    port = 22
    user = 'root'
    password ='******'
    if re.match(pattern, address):
        ssh = paramiko.SSHClient()
        ssh.load_system_host_keys()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(address,port,user,password)
        self.GoodConnect()
    else:
        self.ErrMsg()

def OnSearch(self, somevariable):  
    apath = '/'
    apattern = '"*.txt" -o -name "*.log"' 
    rawcommand = 'find {path} -name "*.txt" -o -name "*.log"' #{pattern}
    command1 = rawcommand.format(path=apath, pattern=apattern)
    stdin, stdout, stderr = ssh.exec_command(command1)
    filelist = stdout.read().splitlines()
    ftp = ssh.open_sftp()

1 个答案:

答案 0 :(得分:1)

您在ssh中定义DispConnect(),然后在未定义的OnSearch()中再次使用它。由于这一切都发生在同一个班级(我假设),请在if re.match...中填写最后一行

self.ssh = ssh

然后在OnSearch()中,使用self.ssh代替ssh

也就是说,您在方法中使用的局部变量在这些方法之外是不可用的。使用self.ssh使其成为类的成员,然后可以在类中的任何位置使用它。