Advertisement
Guest User

Untitled

a guest
May 4th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from paramiko import Transport, SFTPClient
  4.  
  5. """
  6. Wrap Paramiko's SFTP functionality in an easy-to-use
  7. class; hide the need to initialize a transport object
  8. to make a connection.
  9. """
  10.  
  11. class Connection(object):
  12.  
  13.     def __init__(self, server, username, password):
  14.         self.transport = Transport(server)
  15.         self.transport.connect(username= username, password = password)
  16.         self.sftp_session = SFTPClient.from_transport(self.transport)
  17.  
  18.     def listdir(self, path = "."):
  19.         return self.sftp_session.listdir(path)
  20.  
  21.  
  22.     def get_file(self, remote_filename, local_filename):
  23.         self.sftp_session.get(remote_filename, local_filename)
  24.         return True
  25.  
  26.     def put_file(self, local_filename, remote_filename = None):
  27.         if not remote_filename:
  28.             remote_filename = local_filename
  29.         self.sftp_session.put(local_filename, remote_filename)
  30.         return True
  31.  
  32.     def chdir(self, path):
  33.         try:
  34.             self.sftp_session.chdir(path)
  35.         except IOError:
  36.             raise ValueError, "Invalid path specified"
  37.            
  38.         return True
  39.  
  40.     def close(self):
  41.         self.sftp_session.close()
  42.         self.transport.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement