gpt4 book ai didi

python - 为什么 paramiko 偶尔会引发异常?

转载 作者:太空宇宙 更新时间:2023-11-03 16:31:40 24 4
gpt4 key购买 nike

为了方便起见,我编写了一个小包装类来登录远程主机,执行命令,结束检索数据:

def MySSHClient:

def connect(self, remoteHost, remotePort, userName, password):
self.__s = paramiko.SSHClient()
self.__s.load_system_host_keys()
self.__s.connect(remoteHost, remotePort, userName, password)

def exec_command(self, command):
bufsize = -1
chan = self.__s.get_transport().open_session()
chan.exec_command(command)
stdin = chan.makefile('wb', bufsize)
stdout = chan.makefile('r', bufsize)
stderr = chan.makefile_stderr('r', bufsize)
stdin.close()
exitcode = chan.recv_exit_status()
r = MySSHCommandResult(command, stdin, stdout, stderr, exitcode)
chan.close()
return r

def close(self):
self.__s.close()

此代码改编自原始的 paramiko python 实现。我刚刚添加了最后 5 行。

(仅供引用:MySSHCommandResult 在构造期间从 stdout 和 strerr 读取所有数据并将其存储以供进一步使用。)

MySSHClient 类在一个简单的 python 程序中使用:

....

exitCode = 0
s = None
try:
....
exitCode = 3
s = MySSHClient()
s.connect(host, port, login, password)
exitCode = 4
result = s.exec_command(myCommand)
exitCode = 5
if not result.isSuccess():
raise Exception("Failed to execute command!")
result.dump() # for current debugging purposes
exitCode = 0
except:
pass

if s is not None:
s.close()
sys.exit(exitCode)

(通过这些退出代码,Python 程序告诉调用者一切是否成功。如您所见,使用了各种退出代码,以便在失败时进行一些错误诊断。)

到目前为止一切顺利。基本上这是有效的。但我不明白的是,有时我的 python 程序会给出这样的额外输出:

Exception ignored in: <bound method BufferedFile.__del__ of <paramiko.ChannelFile from <paramiko.Channel 0 (closed) ->     <paramiko.Transport at 0x74300588 (unconnected)>>>>
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 61, in __del__
File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 79, in close
File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 88, in flush
TypeError: 'NoneType' object is not callable

或者像这样:

Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 61, in __del__
File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 79, in close
File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 88, in flush
TypeError: 'NoneType' object is not callable

一切都一直工作正常,但大约有 10% 到 20% 的时间我会看到这些错误消息。有谁知道为什么有时清理会在程序终止时失败?如何避免这些错误消息?

最佳答案

由于某种原因,sys.exit时垃圾没有自动清理

要手动强制清除,您只需使用 del 删除分配的对象即可。

这是我的代码:

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, port, username, password)
stdin_raw, stdout_raw, stderr_raw = client.exec_command(cmd)
exit_code = stdout_raw.channel.recv_exit_status()

stdout = []
for line in stdout_raw:
stdout.append(line.strip())

stderr = []
for line in stderr_raw:
stderr.append(line.strip())


# Clean up elements
client.close()
del client, stdin_raw, stdout_raw, stderr_raw


logger.debug("stdout: %s" % stdout)
logger.debug("stderr: %s" % stderr)
logger.debug("exit_code: %s" % exit_code)

请注意这一行:

del client, stdin_raw, stdout_raw, stderr_raw

这是我的来源: https://github.com/paramiko/paramiko/issues/1078#issuecomment-596771584

关于python - 为什么 paramiko 偶尔会引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37556888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com