开发者

How would I call (for eg.) a function to connect to a function to search using wxPython

Below are five functions of my program to connect to an Ubuntu server using Paramiko. I wrote a command line script in Python that handles this perfectly, but I'm attempting to learn wxPython and I'm having a few challenges. The script works fine if I have it one function, but, being a newb, I'm trying to practice to write more efficient code. As the function are, I'm getting a message that "ssh is not defined ..." I've tried passing parameters, and other combination of things ... I guess I'm overlooking something. Can someone assist me with this?

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())
开发者_StackOverflow中文版        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()


You define ssh in DispConnect() but then use it again in OnSearch() where it hasn't been defined. Since this is all taking place in the same class (I assume), make your last line in the if re.match... be

self.ssh = ssh

Then in OnSearch(), use self.ssh instead of ssh.

That is, the local variables you use within methods aren't available outside of those methods. Using self.ssh makes this a member of the class, and it can then be used anywhere within the class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜