SFTP is a secure file transfer protocol used for transferring files over the internet. It helps you to file access, transfer and file management over any reliable data stream.
Python provides a module called PySftp used to connect to the SFTP server. It is a simple interface to SFTP and uses SSH protocol version 2 implementations.
This post will explain how to use PySftp to connect the SFTP server and manage it using Python.
Install PySftp
First, you will need to install Python and other packages to your system. You can install them with the following command:
apt-get install python3 python3-pip -y
Next, install the PySftp using the following command:
pip install pysftp
Access SFTP Server Using PySftp
In this section, we will create a Python script that connects to the remote SFTP server and list files from the specified directory:
Let's create a Python script named sftp.py:
nano sftp.py
Add the following content:
import pysftp
Hostname = "remote-ip-address"
Username = "root"
Password = "password"
with pysftp.Connection(host=Hostname, username=Username, password=Password) as sftp:
print("Connection successfully established ... ")
# Switch to a remote directory
sftp.cwd('/opt/')
# Obtain structure of the remote directory '/opt'
directory_structure = sftp.listdir_attr()
# Print data
for attr in directory_structure:
print(attr.filename, attr)
Save and close the file when you are finished.
In the above script, we have imported the PySftp module then store remote SFTP username, password, and IP address in the variable. Then, we have used Python statements to establish a secure SFTP connection using IP, username, and password stored in the variable. After the successful connection, we will switch the remote directory to /opt and list all files one by one.
You can now run the sftp.py script using the following command:
python3 sftp.py
You should see the following output:
Connection successfully established ...
fstab -rw-r--r-- 1 0 0 558 22 Jul 05:24 fstab
host.conf -rw-r--r-- 1 0 0 92 22 Jul 05:24 host.conf
hostname -rw-r--r-- 1 0 0 11 22 Jul 05:24 hostname
hosts -rw-r--r-- 1 0 0 196 22 Jul 05:24 hosts
hosts.allow -rw-r--r-- 1 0 0 411 22 Jul 05:24 hosts.allow
hosts.deny -rw-r--r-- 1 0 0 711 22 Jul 05:24 hosts.deny
netplan drwxr-xr-x 1 0 0 4096 22 Jul 05:25 netplan
networkd-dispatcher drwxr-xr-x 1 0 0 4096 22 Jul 05:25 networkd-dispatcher
networks -rw-r--r-- 1 0 0 80 22 Jul 05:25 networks
Upload a File to SFTP Using PySftp
If you want to upload a file from your local system to the SFTP server using PySftp, you just need to use the sftp.put() method of the SFTP client.
Let's create a sftp.py script to upload a file named initrd.img located at /boot/initrd.img on the local system to the remote SFTP server in the /mnt directory.
nano sftp.py
Add the following content:
import pysftp
Hostname = "remote-ip-address"
Username = "root"
Password = "password"
with pysftp.Connection(host=Hostname, username=Username, password=Password) as sftp:
print("Connection successfully established ... ")
# Define a file that you want to upload from your local directory
localFilePath = '/boot/initrd.img'
# Define the remote path where the file will be uploaded
remoteFilePath = '/mnt/initrd.img'
# Use put method to upload a file
sftp.put(localFilePath, remoteFilePath)
# Switch to a remote directory
sftp.cwd('/mnt/')
# Obtain structure of the remote directory '/opt'
directory_structure = sftp.listdir_attr()
# Print data
for attr in directory_structure:
print(attr.filename, attr)
Save and close the file when you are finished.
Where:
- localFilePath is the path of the local file.
- remoteFilePath is the path of the remote file.
- sftp.put is the method used to upload a file to the SFTP server.
Next, run the sftp.py script using the following command:
python3 sftp.py
You should get the following output:
Connection successfully established ...
initrd.img -rw-r--r-- 1 0 0 19543043 22 Jul 05:40 initrd.img
Download a File From SFTP Using PySftp
In this section, we will use the sftp.get() method to download a file from the remote SFTP server.
Let's create a sftp.py script using the following command:
nano sftp.py
Add the following content:
import pysftp
Hostname = "remote-ip-address"
Username = "root"
Password = "password"
with pysftp.Connection(host=Hostname, username=Username, password=Password) as sftp:
print("Connection successfully established ... ")
# Define the remote path where the file will be uploaded
remoteFilePath = '/etc/hosts'
# Define a file that you want to upload from your local directorty
localFilePath = '/opt/hosts'
# Use get method to download a file
sftp.get(remoteFilePath, localFilePath)
Save and close the file when you are finished.
Next, run the sftp.py script to download /etc/hosts file from the remote SFTP server to the local system:
python3 sftp.py
You should get the following output:
Connection successfully established ...
Delete a File From SFTP Using PySftp
You can also remove a file on the SFTP server using the sftp.remove() method.
In the following example, we will remove a file named /opt/file1.txt on the SFTP server.
Let's create a sftp.py script as shown below:
nano sftp.py
Add the following content:
import pysftp
Hostname = "remote-ip-address"
Username = "root"
Password = "password"
with pysftp.Connection(host=Hostname, username=Username, password=Password) as sftp:
print("Connection successfully established ... ")
# Define the remote path file path
removeFilePath = '/opt/file1.txt'
sftp.remove(removeFilePath)
Save and close the file, then run it with the following command:
python3 sftp.py
You should get the following output:
Connection successfully established ...
Conclusion
In the above guide, we explained how to use the PySftp Python module to manage and working with SFTP in Python. The PySftp module has a wide range of methods that you can use to do multiple things, like handling permissions, etc.
SFTP server FAQs
How do I download from SFTP server using python?
Python provides a module called pysftp, which provides a range of commands to operate SFTP functions from within a Python script. First, use the Connection method within the module to create a connection then use the get method to download the file. The get method has two parameters: the remote path with the file name on the end of it and the local path, also with the file name on the end.
What ports does sftp use?
SFTP uses one connection (FTPS has two). It is the same port used by SSH, which is TCP port 22.
What is get command in SFTP?
The SFTP get command draws a file from a remote device onto the local device – this is a copy function rather than a file move. The get command has one required parameter, which is the remote path with the file name on the end of it. You can add a local path to the end of the command. If you don’t the file comes into the current directory with the same name as the original file. You can just put a new file name for the local path if you want to rename it.
How do I install the pysftp library in Python?
You can install the pysftp library using pip, a package manager for Python. Open your command prompt or terminal and type the command "pip install pysftp".
What are the steps to connect to an SFTP server in Python?
To connect to an SFTP server in Python, you need to import the pysftp library, create an instance of the pysftp.Connection object, and pass the hostname, username, and password or private key for authentication. Once you have created the connection object, you can use it to perform various operations on the remote SFTP server.
How can I use a private key to authenticate to the SFTP server?
To use a private key for authentication, you can pass the path to the private key file as an argument when creating a connection object. For example:
import pysftp
private_key = "/path/to/private_key"
sftp = pysftp.Connection(host, username=username, private_key=private_key)
How do I authenticate to the SFTP server using a username and password?
To authenticate to the SFTP server using a username and password, you can pass the username and password as arguments when creating a connection object using the pysftp library. For example:
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
sftp = pysftp.Connection(host, username=username, password=password, cnopts=cnopts)
What are some common errors I might encounter when accessing an SFTP server in Python?
Some common errors you might encounter include:
- Connection refused or timed-out errors
- Authentication failures
- Permission denied errors when attempting to read or write files
- File transfer errors are caused by network issues or file permission problems