How to access SFTP server in Python

Table of Contents

SFTP (Secure File Transfer Technology) is a secure file transfer protocol for sending files over the internet. It allows you to access, transmit, and manage files over any secure data stream.

Python and SFTP are two valuable tools when building or maintaining a website. As a web developer, you can utilize SFTP to access your website quickly, transfer files, and even backup your website’s data. Python allows you to construct a powerful back-end automated solution for your entire website, not just for downloading or uploading data to your SFTP server.

We’ll go over the Python modules for using SFTP in this post. Then we’ll use these modules to upload and get data in a few scenarios. Finally, we’ll look at a Python SDK for using an SFTP cloud-based service from a third party.

PySftp is a Python package that allows you to connect to an SFTP server. It employs SSH protocol version 2 implementations and has a simple SFTP interface. This tutorial will show you how to connect to an SFTP server using PySftp and administer it with Python.

Using Python to connect to an SFTP server

The “Secure Shell File Transfer Protocol” is an SFTP (Secure FTP) transferring technique. Further, the SFTP protocol is based on SSH and allows clients and servers to communicate securely over insecure networks by establishing an encrypted tunnel between them.Keep in mind that SFTP differs from FTP over SSH. Although FTP over SSH uses SSH, it’s just plain old FTP wrapped in SSH. SFTP, on the other hand, is a standalone file transfer protocol that leverages SSH and emulates the FTP syntax.

SUGGESTED READ

In Python, what do you need to connect to an SFTP server?

To use Python to connect to an SFTP server, you’ll need the following parameters:

  • The IP address of the server (or hostname)
  • The username and password are required.
  • In most circumstances, you’ll also need an SSH key.

You’ll need to add this information to your Python script as variable names. It’s worth noting that SFTP connections almost always necessitate the use of keys. The public SSH keys can be provided by the SFTP server management or obtained through an SFTP connection’s initial handshake.

How to extract the SSH key file

If you connect to the SFTP server for the first time with credentials, the SFTP server will send you the host’s SSH key. Although your SFTP client is unaware of the key, you are likely familiar with and trust the target SFTP server, so connect. Keep your key in a secure location at all times.

To make an initial connection and obtain the key, you can use SFTP client software such as CyberDuck or FileZilla. Keep the key in a secure location. Be cautious if you receive this warning a second time. There could have been a security compromise in the FTP server if the server public key and your stored public key no longer match.

Python’s mandatory modules

Pysftp is the only Python module you’ll need to connect to an SFTP server. This easy-to-use module provides this simple SFTP interface. This module, however, is reliant on two other modules: Paramiko and Cryptography.

SUGGESTED READ

Note that if you’re using legacy Paramiko (1.x), especially versions 1.13 and higher, you’ll also require PyCrypto. However, upgrading to Paramiko (2.x) and using the Cryptography module is suggested. The latter is because the legacy dependency “PyCryto” has been deprecated and contains security flaws.

Cryptography

Cryptography is a module that implements SSH by providing low-level encryption techniques. This module is a Paramiko dependency. To use pip to install Cryptography, follow these steps:

$ pip install cryptography

Paramiko

Paramiko is a Python implementation of the SSHv2 protocol that can be used as a client or a server. It also uses the pyca/cryptography Python C module for low-level cryptography. Apart from cryptography, Paramiko uses bycrypt and pynacl as direct dependencies.

Paramiko is a beautiful Python library that serves as the foundation for the Pysftp module. You’ll need to upgrade your Python to 2.7 to install Paramiko (or above). Install the most recent stable version by following these steps:

$ pip install paramiko

If you installed Paramiko correctly, you should be able to use the pysftp module.

SUGGESTED READ

A demonstration of the pysftp module

The pysftp module includes an entire library for all of your SFTP needs. As previously stated, pysftp is a Paramiko dependent (it requires Paramiko >= 1.15.2). Pysftp is a thin wrapper of Paramiko’s SFTP client; it does not give all of Paramiko’s functionality but instead implements on top of high-level Paramiko activities.

The connection object “pysftp.Connection()” is the foundation of pysftp. This object can connect to an SFTP server using a username, password, or keys. It also lets you transfer files, delete them, and add them to a list, among other things.

Other pysftp connection routines that are useful

You can also use functions like: pysftp.Connection.get() and the print attribute in addition to pysftp.Connection.get() and the print attribute. Use the line “sftp.put(localFilePath, remoteFilePath)” in pysftp.Connection.put() to upload a local file to a remote path. Pysftp.remove.You may need to delete a file from your remote server. Use the command sftp.remove(‘/var/example-folder/FILE001.txt’) to accomplish this.

To access a cloud-based platform, use your Python app

Files.com is a secure cloud-based file-sharing and workflows solution for businesses of any size. You can exchange files via a highly secure cloud-based platform and do other duties for your sensitive information with Files.com, such as automation, collaboration, and audits.

You can connect to the Files.com platform using three popular transfer methods: FTP, SFTP, and WebDAV. You may also use the same platform to mount or sync your cloud-based storage, such as Amazon S3, Azure, Dropbox, etc.

SUGGESTED READ

Using Files.com as part of your Python application

For Python, Files.com offers a Software Development Kit (SDK). This SDK is a set of development tools that help developers authenticate users, transfer data through SFTP, generate scheduled backups, and more in an easy-to-use Python environment.

Python SDK for Files.com:

  • Allows Python applications to have quick and easy access to the Files.com API.
  • It provides a set of libraries that Python programmers are familiar with.
  • It also includes file-related features such as download, upload, list, writing, reading, etc.
  • All file operations should be performed with the Python SDK.

To get the Files.com SDK for Python, follow these steps.

  • It may be installed via PyPi
  • You can also get it from GitHub directly

Additionally, Files.com can be used as an SFTP client. Your external SFTP server can be mounted as a folder in Files.com and accessed by the web interface, Python’s SDK, API, or other inbound SFTP connections. So, install PySftp if you haven’t already.

You must first install Python and other packages on your computer. You can use the following command to install them:

SUGGESTED READ

apt-get install python3 python3-pip -y

Next, use the following command to install PySftp:

pip install pysftp

Using PySftp, connect to an SFTP server

In this section, we’ll write a Python script to connect to a remote SFTP server and list files in a specified directory. Let’s make a sftp.py script in Python:

vim sftp.py

Include the following information:

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)

When you’re finished, save and close the file.

The PySftp module is loaded in the preceding script, and the remote SFTP username, password, and IP address are stored in the variable. Then, using Python instructions, we created a secure SFTP connection with the IP, username, and password stored in the variable. After a successful connection, we’ll change the remote directory to ‘/opt’ and go through each file individually.

SUGGESTED READ

You can now use the following command to start the sftp.py script:

python3 sftp.py

Using PySftp to Upload a File to SFTP

You can use PySftp to upload a file from your local system to the SFTP server by calling the SFTP client’s sftp.put() method. Let’s write a sftp.py script to upload initrd.img from the local system’s ‘/boot/initrd.img’ directory to the remote SFTP server’s ‘/mnt’ directory.

vim sftp.py

Include the following information:

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 of your choice that you want to upload from your local directory
local_file_path = '/boot/initrd.img' # Define the desired remote path for the file to be uploaded to
remote_file_path = '/mnt/initrd.img' # Engage the put method when you want to upload a file
sftp.put(local_file_path, remote_file_path) # Subsequetnly, change directories to your remote directory
sftp.cwd('/mnt/') # Now get the remote directory's structure '/opt'
directory_structure = sftp.listdir_attr() # Printing of the data
for attr in directory_structure: print(attr.filename, attr)

When you’re finished, save and close the file.

Where:

SUGGESTED READ

  • The path to the local file is specified by local_file_path.
  • The path to the remote file is specified by remote_file_path.
  • The method sftp.put is used to upload a file to an SFTP server.

Then, using the following command, launch the sftp.py script:

python3 sftp.py

Using PySftp, you can download a file from an SFTP server

We’ll utilize the sftp.get() function to download a file from a remote SFTP server in this section. Let’s make a sftp.py script with the commands below:

vim sftp.py

Include the following information:

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
remote_file_path = "" # access the hosts under the etc directory # Define a file that you want to upload from your local directory
local_file_path = '/opt/hosts' # Use get method to download a file
sftp.get(remote_file_path, local_file_path)

When you’re finished, save and close the file. To download the host’s file under the etc directory from the distant SFTP server to the local system, run the sftp.py script:

python3 sftp.py

Using PySftp, delete a file from SFTP

The sftp.remove() method can also remove a file from an SFTP server. On the SFTP server, we will delete a file named ‘/opt/file1.txt’ in the example below. Let’s make a sftp.py script like this:

SUGGESTED READ

vim sftp.py

Include the following information:

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
remove_file_path = '/opt/file1.txt' sftp.remove(remove_file_path)

Save and shut the file before running it with the command:

python3 sftp.py

What now?

To get started with SFTP in Python, you’ll need to obtain the server’s IP address, credentials (username and password), and public key. You’ll need to add these variables in your Python script. Then, to use Python SFTP, you’ll need to install the Pysftp module, which is required for all SFTP operations and is dependent on two other modules: Paramiko and Cryptography.

A sophisticated third-party SFTP solution can also be used to empower your Python apps. Files.com, for example, is a cloud-based file-sharing service with a Python SDK for integration into your apps.

Conclusion

The previous section covered how to utilize the PySftp Python module to manage and deal with SFTP in Python. The PySftp module contains several methods that can be used to do various tasks, such as handling permissions.

SUGGESTED READ

Handling things via SFTP with your scripts can be quite helpful, and if you’re working with Python, PySftp is the package you’ll need to work with this technology without any headaches because it’s straightforward to use. pysftp is a wrapper for Paramiko that provides a more Python-like interface.

The Paramiko library is a fantastic Python package that serves as the foundation for pysftp.

The methods offered by pysftp are abstractions that help programmers be more productive by encapsulating many of the more advanced SFTP use cases. Rather than writing your code to walk directories and call get and put, you may use paramiko and Python’s native os and stat modules and write tests. Many online code snippets are incomplete and fail to account for edge circumstances. However, pysftp comes with a complete library to deal with all three. Allowing you to concentrate on your main task.

Source: https://www.codeunderscored.com/how-to-access-sftp-server-in-python/


You might also like this video