在Python中为多个函数创建单个TELNET对象
这个问题在这里已经有了答案:
  如果您问如何在所有函数中引用同一个telnet对象,则需要使用全局变量。  添加声明“全球TN”内部结合的任何功能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
上一篇: Create single TELNET object for multiple functions in Python
下一篇: Python function replacing itself in the module namespace
