Python类调用属于同一个类的函数

时间:2017-06-03 15:35:16

标签: python python-2.7

我正在尝试调用属于同一函数的函数。以下是代码

class Asrconfig():
    def __init__(self, hostname, net_username, net_password):

        ''' SSH connection Establish '''
        self.hostname = hostname
        self.net_username = net_username
        self.net_password = net_password
    def connect(self, cmd):
        print "connect"
        self.cmd = cmd
        self.remote_conn_pre = paramiko.SSHClient()
        self.remote_conn_pre.set_missing_host_key_policy(
            paramiko.AutoAddPolicy())
        self.remote_conn_pre.connect(self.hostname, username=self.net_username,
            password=self.net_password,look_for_keys=False, allow_agent=False)
        self.remote_conn = self.remote_conn_pre.invoke_shell()
        buff = ''
        remote_conn.send(cmd)
        while not buff.endswith('#'):
            resp = remote_conn.recv(9999)
            buff += resp
            print resp
    def inventory(self, cmd):
        cmd = "show inventory"
        self.connect(cmd)
asr = Asrconfig(hostname, net_username, net_password)
asr.inventory()

运行输出时,我的输出低于输出值:

RP#
Traceback (most recent call last):
  File "onboard/testpara1.py", line 43, in <module>
    asr.inventory()
TypeError: inventory() takes exactly 2 arguments (1 given)

我需要以下帮助

我无法在库存功能下的类Asrconfig中调用connect函数。

2 个答案:

答案 0 :(得分:0)

重写您的inventory方法,如下所示:

def inventory(self, cmd="show inventory"):
    self.connect(cmd)

答案 1 :(得分:0)

您的问题的解决方案:

class Asrconfig:
def __init__(self, hostname, net_username, net_password):
    ''' SSH connection Establish '''
    self.hostname = hostname
    self.net_username = net_username
    self.net_password = net_password
def connect(self, cmd):
    print "connect"
    self.cmd = cmd
    self.remote_conn_pre = paramiko.SSHClient()
    self.remote_conn_pre.set_missing_host_key_policy(
        paramiko.AutoAddPolicy())
    self.remote_conn_pre.connect(self.hostname, username=self.net_username,
        password=self.net_password,look_for_keys=False, allow_agent=False)
    self.remote_conn = self.remote_conn_pre.invoke_shell()
    buff = ''
    remote_conn.send(cmd)
    while not buff.endswith('#'):
        resp = remote_conn.recv(9999)
        buff += resp
        print resp
def inventory(self, cmd):
    cmd = "show inventory"
    self.connect(cmd)  

asr = Asrconfig(hostname, net_username, net_password)
# You need to pass parameter in inventory() method.
asr.inventory("Hello kitty...")