如何解决当 SFTP 连接关闭时,为什么 SSH 会话不活跃或不活跃?
根据下面的文章,我正在尝试使用 Object finalizer
来:
- 创建到服务器的 SSH 连接。
- 通过 SFTP 连接。
- 将本地对象上传到服务器。
- 终止连接。
但是,连接似乎在没有提示的情况下关闭。知道是什么原因造成的吗?
https://extsoft.pro/safely-destroying-connections-in-python/
Exception ignored in: <function SshConnection.__del__ at 0x10bbbf0d0>
Traceback (most recent call last):
File "redacted.py",line 49,in __del__
self._client.open_sftp().close()
File "/Users/red/Library/Python/3.8/lib/python/site-packages/paramiko/client.py",line 556,in open_sftp
return self._transport.open_sftp_client()
File "/Users/red/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py",line 1097,in open_sftp_client
return SFTPClient.from_transport(self)
File "/Users/red/Library/Python/3.8/lib/python/site-packages/paramiko/sftp_client.py",line 164,in from_transport
chan = t.open_session(
File "/Users/red/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py",line 875,in open_session
return self.open_channel(
File "/Users/red/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py",line 969,in open_channel
raise SSHException("SSH session not active")
paramiko.ssh_exception.SSHException: SSH session not active
我导入的类:
import paramiko
class SshConnection:
def __init__(self,sftp_host,sftp_username,stfp_pkey):
self._client = paramiko.SSHClient()
self._client.load_system_host_keys()
self._client.connect(sftp_host,username=sftp_username,pkey=stfp_pkey)
def __del__(self):
if self._client:
self._client.open_sftp().close()
self._client.close()
def output(self):
return self._client.open_sftp()
主程序:
import SshConnection
def main(self):
choice = True
while choice:
choice = input("\nWhat would you like to do?\n"
"Redacted")
elif choice == "4":
connection = SshConnection(self.host_dict[self.environment],self.sftp_user,self.private_key)
print(connection.output().put(self.source_file,self.destination_file))
print("Upload complete. Checking if updated.\n")
check_results()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。