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

发布于 2024-12-04 08:41:57 字数 2084 浏览 0 评论 0原文

下面是我的程序使用 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()

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())
        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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

焚却相思 2024-12-11 08:41:57

您在 DispConnect() 中定义了 ssh,但随后在尚未定义的 OnSearch() 中再次使用它。由于这一切都发生在同一个类中(我假设),因此将 if re.match... 中的最后一行设置为

self.ssh = ssh

然后在 OnSearch() 中,使用 self.ssh< /code> 而不是 ssh

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文