python - 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')
但我看不到在哪里。有什么问题(可能是缩进?)。谢谢
解决方案
您正在将“通道”作为实例变量访问,在调用之前创建__init__
或调用。_create_channel
_send_cmd_to_channel
也参考这个
推荐阅读
- swift - 在自定义 UITabBarController (Swift) 中以编程方式导航
- react-native - 从 Expo 错误中弹出 - 未找到 SDK 位置
- plot - 鼠尾草的 3dplot 中的 z 轴太长
- javascript - 如果文件是带有 Vue.js 的音频或视频文件,我如何根据 url 检查
- python - Kubernetes pod 中的名称解析失败
- c - 在其他文件中以 typedef 作为参数的函数声明
- html - 代码未在 Html (Flask) 中执行和显示
- postgresql - 无法在 Postgres 中将 JSONB 标量解析为数组
- swift - 如何在 SwiftUI 中从视图中心创建动画颜色变化?
- python - Python/Numpy/Boolean Indexing:对于数组中的每个 True 值,修改接下来的 2 个元素