SFTP Files Transfer Using Python

Rajan Sahu
4 min readFeb 25, 2023

SFTP (Secure File Transfer Protocol) is a secure method of transferring files over the internet. Paramiko is a Python library that provides a secure way to communicate with remote servers using various protocols, including SFTP. In this blog, we will discuss how to use Paramiko to connect, transfer, download and remove files securely using SFTP.

There are two libraries for playing with SFTP file transfer in Python

1. Paramiko: It is well-managed and continuously updated to support the new version of Python.

2. Pysftp: But in the case of Pysftp we can get less than Python 3.5 version.

For this article, we are going to use Paramiko to demonstrate how to connect, transfer, upload and remove files from an SFTP server.

Setting Up Paramiko:

  • Before we can use Paramiko, we need to install it.
  • Paramiko can be installed using pip, the Python package installer.
  • To install Paramiko, open a command prompt or terminal window and run the following command:
pip install paramiko

Establishing Connecting to the Server:

import paramiko

# create ssh client
ssh_client = paramiko.SSHClient()

# remote server credentials
host = "hostname"
username = "username"
password = "password"
port = port

ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host,port=port,username=username,password=password)

print('connection established successfully')

ssh_client.close()
  • SHH Client: We can use the Secure Shell Protocol, SSHClient() method of the Paramiko package to create an SSH client in our program. SSH clients are used to communicating with an SFTP server for the transfer of files.
  • ssh_client.set_missing_host_key_policy(): If we call ssh_client.set_missing_host_key_policy(), and we pass “paramiko.AutoAddPolicy()” as an argument, then Paramiko will make our machine trust incoming traffic from our remote server.

List down all the directories in the SFTP

import paramiko

# create ssh client
ssh_client = paramiko.SSHClient()

# remote server credentials
host = "hostname"
username = "username"
password = "password"
port = port

ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host,port=port,username=username,password=password)

ftp = ssh_client.open_sftp()
files = ftp.listdir()

print("Listing all the files and Directory: ",files)

# close the connection
ftp.close()
ssh_client.close()
  • Using the above code snippets we can list down all the existing files and directories in the SFTP server.

Uploading a File to the SFTP server:

To upload a file to the remote server using SFTP, we can use the SFTP client objects put method. Here's an example:

import paramiko

# create ssh client
ssh_client = paramiko.SSHClient()

# remote server credentials
host = "hostname"
username = "username"
password = "password"
port = port

ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host,port=port,username=username,password=password)

# create an SFTP client object
ftp = ssh_client.open_sftp()

# download a file from the remote server
files = ftp.put("local_file_path","remote_file_path")

# close the connection
ftp.close()
ssh_client.close()
  • Above mentioned code snippets are used to upload a file from the local file path to the remote server’s file path.
  • The put method takes two arguments: the local file path and the remote file path.

Downloading a File from the SFTP location:

  • To download a file from the remote server using SFTP, we can use the SFTP client objects get method. Here's an example:
import paramiko

# create ssh client
ssh_client = paramiko.SSHClient()

# remote server credentials
host = "hostname"
username = "username"
password = "password"
port = port

ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host,port=port,username=username,password=password)

ftp = ssh_client.open_sftp()
files = ftp.get("remote_file_path","local_file_path")

# close the connection
ftp.close()
ssh_client.close()
  • Using the method, we can download a file from the remote server’s file path to the local file path.
  • The get method takes two arguments: the remote file path and the local file path.

Remove file from SFTP remote location

  • To remove a file from the remote server using SFTP, we can use the SFTP client object’s remove method. Here's an example:
import paramiko

# create ssh client
ssh_client = paramiko.SSHClient()

# remote server credentials
host = "hostname"
username = "username"
password = "password"
port = port

ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host,port=port,username=username,password=password)

ftp = ssh_client.open_sftp()

files = ftp.remove("remote_file_path")

# close the connection
ftp.close()
ssh_client.close()

Create a directory in the SFTP server

  • To create a directory in the SFTP server, we can use the SFTP client object’s mkdir method. Here's an example:
import paramiko

# create ssh client
ssh_client = paramiko.SSHClient()

# remote server credentials
host = "hostname"
username = "username"
password = "password"
port = port

ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host,port=port,username=username,password=password)

ftp = ssh_client.open_sftp()

files = ftp.mkdir("remote_file_path/directory_name")

# close the connection
ftp.close()
ssh_client.close()

Conclusion:

Paramiko is a powerful Python library that provides a secure way to communicate with remote servers using various protocols, including SFTP. With Paramiko, we can easily upload and download files securely to and from remote servers using SFTP.

Reference:

Github:

Thank you for reading. If you find something wrong or better ways to do it, let me know in the comments below.

If you like the post, hit the 👏 button below so that others may find it useful. You can follow me on GitHub and connect with me on Linkedin.

--

--

Rajan Sahu

Backend and Data Engineer by Day; Teacher, Friend and Content-Writer by night.