Create single TELNET object for multiple functions in Python
This question already has an answer here:
If you are asking how to reference the same telnet object in all of the functions, you need to use a global variable. Add the declaration "global tn" inside any functions which binds tn
to a new object.
Try this:
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
链接地址: http://www.djcxy.com/p/23902.html