2011-09-09 15 views
1

Paramikoを使用してUbuntuサーバーに接続する私のプログラムの5つの関数は次のとおりです。私はこれを完全に処理するPythonのコマンドラインスクリプトを書いたが、私はwxPythonを勉強しようとしており、私はいくつかの課題を抱えている。私はそれが1つの関数を持っていればスクリプトは正常に動作しますが、newbであるので、より効率的なコードを書くように練習しようとしています。関数としては、 "sshは定義されていません"というメッセージが出てきます。パラメータを渡してみたり、他のものを組み合わせたりしています...何かを見落としているようです。誰かが私にこれを助けることができますか?wxPythonを使用して検索する関数に関数を呼び出す(例)関数を呼び出すにはどうすればいいですか?

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

あなたはDispConnect()sshを定義しますが、それは定義されていないOnSearch()で再びそれを使用しています。すべて同じクラスで行われているこのので、(私は仮定)、if re.match...であなたの最後の行は、)OnSearch(中その後

self.ssh = ssh 

なる代わりにsshself.sshを使用します。

つまり、メソッド内で使用するローカル変数は、それらのメソッドの外部では使用できません。 self.sshを使用すると、これがクラスのメンバーになり、クラス内のどこでも使用できます。

+0

同じエラーsshが定義されていません。 – suffa

+0

特定のメンバー(私のsolnに続く)を持たないクラスがAttributeErrorを呼び出す必要があり、NameErrorを与えなければならない既存の名前がないので、同じエラーが発生するはずはありません。とにかく、名前の範囲とそれが定義されているときの2つの問題だけがあり、私は上記の解決策について言及しました。それを超えて、それはちょうどデバッグです、私はそのような限られた情報ではできません。 – tom10

関連する問題