我将如何调用(例如)一个函数来连接到一个函数以使用 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()
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
DispConnect()
中定义了ssh
,但随后在尚未定义的OnSearch()
中再次使用它。由于这一切都发生在同一个类中(我假设),因此将if re.match...
中的最后一行设置为然后在 OnSearch() 中,使用
self.ssh< /code> 而不是
ssh
。也就是说,您在方法中使用的局部变量在这些方法之外不可用。使用 self.ssh 使其成为类的成员,然后可以在类中的任何位置使用它。
You define
ssh
inDispConnect()
but then use it again inOnSearch()
where it hasn't been defined. Since this is all taking place in the same class (I assume), make your last line in theif re.match...
beThen in OnSearch(), use
self.ssh
instead ofssh
.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.