首页 > 解决方案 > AttributeError: '对象没有属性'

问题描述

我有以下代码块:

class HwSwitch(object):

    def __init__(self):
        pass

    def _create_channel(self):
        try:
            self.channel = self.ssh.invoke_shell()
        except SSHException:
            raise SSHException("Unable to invoke the SSH Command shell")

    def _send_cmd_to_channel(self, cmd):
        try:
            time.sleep(1)
            self.channel.send(cmd + '\r\n')
            out = self.channel.recv(9999)
        except SSHException:
            raise SSHException("Execution of command '%s' failed" % cmd)
        return str(out)

但我总是收到错误消息:AttributeError: 'HwSwitch' object has no attribute 'channel'。似乎问题出在某个地方,self.channel.send(cmd + '\r\n')但我看不到在哪里。有什么问题(可能是缩进?)。谢谢

标签: pythonpython-3.x

解决方案


您正在将“通道”作为实例变量访问,在调用之前创建__init__或调用。_create_channel_send_cmd_to_channel

也参考这个


推荐阅读