2016-06-24 15 views
0

複数の関数で使用される単一のTelnetオブジェクトを作成したい。Pythonで複数の関数用に単一のTELNETオブジェクトを作成する

tn = (Telnet object declaration only) 

def Function1(): #for connection only 
    tn = telnetlib.Telnet(ip) 
    #code 

def Function2(): #to run command 1 
    tn.write() 

def Function3(): #to run command 2 
    tn.write() 

Function1() #Call for telnet connection 
Function2() #Call to execute command 1 
Function3() #call to execute command 2 

どのような解決方法がありますか?

答えて

0

すべての関数で同じtelnetオブジェクトを参照する方法を尋ねる場合は、グローバル変数を使用する必要があります。 tnを新しいオブジェクトにバインドする関数内で宣言 "global tn"を追加します。

これを試してみてください:

import telnetlib 

tn = None 

def Function1(): #for connection only 
    global tn 
    tn = telnetlib.Telnet(ip) 
    #code 

def Function2(): #to run command 1 
    tn.write() 

def Function3(): #to run command 2 
    tn.write() 

Function1() #Call for telnet connection 
Function2() #Call to execute command 1 
Function3() #call to execute command 2 
関連する問題