【发布时间】:2019-02-20 14:59:51
【问题描述】:
我有以下代码块:
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') 的某个地方,但我看不到在哪里。有什么问题(可能是缩进?)。谢谢
【问题讨论】:
-
你能添加一个运行代码的例子吗?我想你还没有打电话给
_create_channel -
注:如果是缩进,则会引发 IndentationError 或 SyntaxError,而不是 AttributeError。
-
您只在
_create_channel方法中设置self.channel,而不是在__init__构造函数中。这意味着如果你在不调用_create_channel方法的情况下触摸它,你将面临这个错误,我猜这会发生什么。要么添加到__init__,要么从__init__调用_create_channel
标签: python python-3.x