less than 1 minute read

패스워드로

    def _connection_pwd(self):
        try:
            self.connection = paramiko.Transport((config.server[self.server_name]['hostname'],
                                                  config.server[self.server_name]['port']))

            self.connection.connect(username=config.server[self.server_name]['username'],
                                    password=config.server[self.server_name]['password'])
        except Exception as e:
            raise e

key 로

    def _connection_key(self):
        try:
            self.connection = paramiko.Transport((config.server[self.server_name]['hostname'],
                                                  config.server[self.server_name]['port']))
            sftp_key = paramiko.RSAKey.from_private_key_file(config.server[self.server_name]['key'])

            self.connection.connect(username=config.server[self.server_name]['username'],
                                    pkey=sftp_key)
        except Exception as e:
            raise e

실제로 파일을 보내고 전송진행상황을 콜백으로 알려줌

    def transport_file(self, local_path, file_path):
        sftp = paramiko.SFTPClient.from_transport(self.connection)
        sftp.put(local_path, file_path, self._print_total)
        sftp.close()

    def _print_total(self, trans_ferred, toBeTrans_ferred):
        print("Transferred: {0}\tOut of: {1}".format(trans_ferred, toBeTrans_ferred))

Categories:

Updated: