Skip to content

Commit

Permalink
reconnect method and new connect methods in sftp class
Browse files Browse the repository at this point in the history
  • Loading branch information
RichieHakim committed Apr 8, 2024
1 parent c052105 commit b7cf8c8
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions bnpm/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,14 @@ def __init__(
self.transport = paramiko.Transport((hostname, port)) ## open a transport object
else:
if isinstance(ssh_client, ssh_interface):
client = ssh_client.client
self.sftp = client.open_sftp()
self.client = ssh_client
self.sftp = self.client.open_sftp()
elif isinstance(ssh_client, paramiko.SSHClient):
client = ssh_client
self.sftp = client.open_sftp()
self.client = ssh_client
self.sftp = self.client.open_sftp()
elif isinstance(ssh_client, paramiko.Channel):
self.sftp = paramiko.SFTPClient.from_transport(ssh_client.get_transport())
self.transport = ssh_client.get_transport()
self.sftp = paramiko.SFTPClient.from_transport(self.transport)

def connect(
self,
Expand All @@ -642,8 +643,13 @@ def connect(
Password to log in with.
Is not stored.
"""
self.transport.connect(None, username, password) ## authorization
self.sftp = paramiko.SFTPClient.from_transport(self.transport) ## open sftp
if hasattr(self, 'transport'):
self.transport.connect(None, username, password) ## authorization
self.sftp = paramiko.SFTPClient.from_transport(self.transport) ## open sftp
elif hasattr(self, 'client'):
self.sftp = self.client.open_sftp()
else:
raise ValueError('No valid connection method found')

def put_dir(self, source, target, mkdir=True, verbose=True):
'''
Expand Down Expand Up @@ -1003,9 +1009,15 @@ def conj_func(a,b):
).download()

def close(self):
self.sftp.close()
self.transport.close()

if hasattr(self, 'transport'):
self.transport.close()
elif hasattr(self, 'client'):
self.client.close()
elif hasattr(self, 'sftp'):
self.sftp.close()

def refresh_connection(self):
self.connect()

def make_rsync_command(
source,
Expand Down

0 comments on commit b7cf8c8

Please sign in to comment.