AttributeError:'对象没有属性'

2024-10-03 02:38:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码块:

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'对象没有属性'channel'。 问题似乎出在self.channel.send(cmd + '\r\n')的某个地方,但我看不到哪里。有什么问题吗(可能是压痕?)。谢谢


Tags: to代码selfcmdsenddefchannelshell
1条回答
网友
1楼 · 发布于 2024-10-03 02:38:42

您正在将'channel'作为实例变量访问,要么在__init__中创建它,要么在调用_send_cmd_to_channel之前调用_create_channel。在

另请参考this

相关问题 更多 >