Using Python pysftp – Download, Upload files
Today in this article, we will see how to use Python pysftp – Download, Upload files via SFTP. We will retrieve a file from a server via SFTP and upload the file to a remote server using a package library called pysftp.
Today in this article, we will cover below aspects,
pysftp is a simple interface to SFTP and provides abstractions and task-based routines to perform SFTP file upload or download operations.
It is basically a Python library that provides an interface to interact with remote servers using the SSH File Transfer Protocol (SFTP).
It allows you to securely transfer files and perform various operations on remote SFTP servers programmatically.
Installing the Python pysftp package
Let’s install the ‘ pysftp ’ package to connect and pass commands to the SFTP server.
pip install pysftp
Or create a requirements.txt file and declare your dependencies.
pysftp >= 0.2.6
pip install -r requirements.txt
SSH Client – Import pysftp
We can use the Secure method of the pysftp package. SSH clients can be used to communicate with an SFTP server for the transfer of files.
So, we import our installed package Paramiko and create the SSH client in the following manner.
Let’s add the required import statement in the python file,
import pysftp
Define custom SFTPCLient class
To simplify the python code implementation, we will create a simple class called SFTPClient.
class SFTPClient: def __init__(self, hostname, username, password, port):
SFTP Secured Connection using pysftp
Let’s define the connection and startup class below,
class SFTPClient:
def __init__(self, hostname, username, password, port):
self.hostname = hostname
self.username = username
self.password = password
self.port = port
self.connection = None
Note: SFTP connection using other authentication methods, such as SSH key-based authentication is recommended. Kindly refer to more details here.
Define connect method – pysftp
def Connect(self):
self.connection = pysftp.Connection(
host=self.hostname,
username=self.username,
password=self.password,
port=self.port,
)
SFTP Upload file – pysftp
Let’s define the upload file method,
def upload(self, local_path, remote_path):
self.connection.put(localpath=local_path,
remotepath=remote_path)
SFTP Download file – pysftp
Let’s define the download file method,
def download(self, remote_path, local_path):
self.connection.get(remote_path, local_path,
callback=None)
SFTP Close connection – pysftp
if you need to explicitly close the connection, you can use the close connection method defined below,
def close(self):
self.connection.close()
That’s all now you can use the FTP client class defined above to Upload or Download files easily as below,
Upload files,
client = SFTPClient(host, port,
username, password)
client.upload(upload_local_path,
upload_remote_path)
client.close()
Download files,
client = SFTPClient(host, port,
username, password)
client.download(download_remote_path,
download_local_path)
client.close()
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.