How to transfer file from SFTP server to Local using Python

There are situation you may need to programmatically transfer file from SFTP server to you local environment. Here we will see a code written in Linux environment using Python.

Source Code

from paramiko import Transport, SFTPClient
host = 'myftp.company.com'
port = 22
username = 'my_username'
password = 'my_password'
transport = Transport(sock=(host, port))
transport.connect(username=username, password=password)
connection = SFTPClient.from_transport(transport)
connection.listdir()# Will List all the files 
connection.get('myfilename.csv', '/mnt/freshers_in/myfilename.csv', callback=None)

You need to changed the content in BOLD.

Things need to know.
SFTP –  SSH File Transfer Protocol, or Secure File Transfer Protocol, is a separate protocol packaged with SSH that works in a similar way but over a secure connection. Default PORT is 22.

Paramiko is a Python (2.7, 3.4+) implementation of the SSHv2 protocol [1], providing both client and server functionality.

SFTPClient : Documentation

You can install paramiko as follows

pip install paramiko

Author: user

Leave a Reply