Create single TELNET object for multiple functions in Python

This question already has an answer here:

  • Using global variables in a function other than the one that created them 18 answers

  • 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

    上一篇: 标题下的NavigationBar上的UiSegmentedControl

    下一篇: 在Python中为多个函数创建单个TELNET对象