We may earn a commission if you make a purchase through the links on our website.
How to access SFTP server in Python
UPDATED: December 8, 2023
Python and SFTP are two handy tools when you are developing or maintaining a website. As a web developer, you can use SFTP for quick website access, transfer files, and even perform scheduled backups of your website's data. Plus, with Python, you can’t only download or upload files into your SFTP server, but you can create a robust backend automated solution for your entire website.
In this post, we’ll go through the Python modules for using SFTP. Then, we’ll use these modules in a couple of examples for uploading and downloading data. Finally, we’ll go through a Python SDK for accessing a robust third-party SFTP cloud-based solution.
Table of Contents
- Accessing SFTP in Python
- What do you need to access an SFTP server in Python?
- Extracting the SSH key file
- The Python required modules
- Cryptography
- Paramiko
- The pysftp module and an example
- What is pysftp?
- Common Tasks to Perform Using Python pysftp module
- An Example
- Example scripts to list and get
- Download a file
- Other useful connection functions
- Use your Python app to access a cloud-based platform
- Integrating Files.com with your Python application
- Final Words
1. Accessing SFTP in Python
SFTP (Secure FTP) is a transferring mechanism called “Secure Shell File Transfer Protocol.” The SFTP protocol is built upon SSH to establish an encrypted tunnel between client and server and transfer files securely across insecure networks.
Bear in mind that SFTP is not the same as FTP over SSH. Although FTP over SSH also uses SSH, it is the simple old FTP encapsulated with SSH. SFTP, on the other hand, is a complete standalone file transfer protocol that emulates the FTP syntax and uses SSH.
a. What do you need to access an SFTP server in Python?
To access the SFTP server using Python, you’ll need the following parameters:
- The server’s IP address (or hostname)
- The username and password
- And in most cases, an SSH key
You’ll need to get this data to your Python script and assign them as variable names.
Notice that in most cases, SFTP connections require keys. The public SSH keys can be provided by the SFTP server manager or collected from an initial handshake via an SFTP connection.
b. Extracting the SSH key file
If you are connecting to the SFTP server for the first time with credentials, you’ll receive the host’s SSH key from the SFTP server. Your SFTP client doesn’t know about the key, but you likely recognize and trust the target SFTP server, so then establish a connection. Always keep your key in a safe place.
You can use SFTP client tools like CyberDuck or FileZilla to establish an initial connection and receive the key.
Store the key in a safe place. If you were to receive this warning a second time, be careful. If the server public key and your stored public key don’t match anymore, there could have been a breach in the FTP server security.
2. The Python required modules
The only module that you would need for accessing the SFTP server in Python is Pysftp. This easy-to-use module provides a simple interface to SFTP. However, this module depends on two other modules, Paramiko and Cryptography.
Note. If you are running legacy Paramiko (1.x), especially version 1.13 and above, you’ll need another dependency known as PyCrypto. However, it is recommended to upgrade to Paramiko (2.x) and use the Cryptography module. This is because legacy dependency “PyCryto” is deprecated and contains security vulnerabilities.
a. Cryptography
Cryptography is a module that provides the low-level encryption algorithms to implement SSH. This module is a dependency of Paramiko. To install Cryptography with pip:
- $ pip install cryptography
b. Paramiko
Paramiko is a Python implementation for the SSHv2 protocol that provides both client and server functionality. In addition, it leverages a Python C extension for low-level Cryptography (pyca/cryptography). Aside from Cryptography, Paramiko has a few other direct dependencies: bycrypt and pynacl.
Paramiko is a fantastic Python library and is the core for the vital module, Pysftp. To install Paramiko, you’ll need to upgrade your Python to 2.7 (or above). Install the latest stable version with:
- $ pip install paramiko
If you successfully installed Paramiko, you should be able to run the pysftp module.
3. The pysftp module and an example
The pysftp module provides the complete library to handle all your SFTP needs. As mentioned before, pysftp is a dependency of Paramiko (It requires paramiko >= 1.15.2). The Pysftp interface is a thin wrapper of Paramiko’s SFTP client, so it does not provide all the Paramiko features, but rather, it implements on top of high-level Paramiko tasks.
Install Pysftp in your environment with
- $ pip install pysftp
The pysftp bases on the connection object “pysftp.Connection()”. This object supports SFTP connections via username, password, or keys. It also allows you to transfer files, remove, list, among other tasks.
a. What is pysftp?
Using your own scripts to handle SFTP can always be useful. PySftp is a very user-friendly Python library that will allow you to interact with this technology without experiencing any difficulties.
The pysftp module is a Python library that offers high-level abstractions and interface to manage the Secure File Transfer Protocol (SFTP). SFTP, as we all know, is a secure encrypted protocol used by most people to transfer files over an unsecured network, usually in a client-server architecture. A secure channel, such as SSH, is used by the software to operate, where the client's identity is accessible to the protocol and the server has already verified the client.
No security or authentication is supported by the SFTP protocol because it requires the underlying protocol to protect it. The most popular use of SFTP is as a sub-system implementation of SSH protocol version 2, which was created by the same working group.
Because of pysftp working with remote files and directories is made easier and is accessible to developers with prior Python experience.
You can use the pysftp module to create Python scripts that automate SFTP functions on remote servers that support SFTP. These tasks include file uploading and downloads, remote directory management, and other file-related processes on remote servers that support SFTP.
The pysftp module has an interface heavily influenced by Python and functions as a wrapper around Paramiko – the best Python libraries. Pysftp's methods are abstractions that encapsulate several higher function use cases of communicating with SFTP, hence enhancing a programmer's productivity.
As an alternative to constructing the script to traverse directories and invoke get and put, it supports handling not only Paramiko but also the OS and stat modules of Python and write tests. The pysftp module offers a comprehensive library to handle each of the three, allowing users to concentrate on other important duties.
b. Common Tasks to Perform Using Python pysftp module
- Permissions Access to set or modify the remote files and directories permissions
- Listing Files Python pysftp module allows users to list down all their directories and files on a remote server
- Directory Manipulation Facilitates features to generate, remove, or remote directories.
- Secure File Transfer Be it moving files between local machines or remote servers, you can easily do the file transfer securely using SFTP.
- Error Management Python SFTP offers error management features to handle issues that may arise during SFTP operations in a graceful manner.
- File Operations Users gain access to perform various tasks like checking, renaming, or deleting existing remote files.
When working with the Secure File Transfer Protocol (SFTP) in applications and automation tasks, developers and system administrators can benefit from a number of features provided by the Python pysftp package.
You can create complex processes that incorporate SFTP transfers as part of a more comprehensive automation process by integrating pysftp with other Python libraries and utilities. Further, scripts can be written to automatically move data between local and remote servers, synchronize files, and schedule backups.
c. An example
An example of using pysftp with the connection object:
import pysftp
with pysftp.Connection(‘hostname', username='me', password='secret') as sftp: with sftp.cd(‘public'): # temporarily chdir to public sftp.put(‘/my/local/filename') # upload file to public/ on remote sftp.get(‘remote_file') # get a remote file |
Variables like hostname (IP or host), username, and password must be defined in addition to directory and names of local and remote files. For example, to upload an “examplefile.txt” from a local path to a remote path “/var/www/html/examplefile.txt,” use the sftp.put (localpath,remotepath).
with pysftp.Connection(‘hostname', username='me', password='secret') as sftp:
remotepath='/var/www/html/examplefile.txt' localpath='examplefile.txt' sftp.put(localpath,remotepath) |
4. Example Scripts to List and Get
In the following different Python examples, pysftp is used to check a key file when attempting to connect to the SFTP host and provides a list of files and paths.
import pysftp
import os localFile = os.path.join(filepath, filename) cnopts = pysftp.CnOpts(knownhosts=os.path.join(filepath, “keyfile”)) Hostname = “hostname” Username = “username” Passsword = “password” with pysftp.Connection(host=Hostname, username=Username, password=Password, cnopts=cnopts) as sftp: print “Connection succesfully established … ” # change to a remote directory sftp.cwd(‘/var/www/sftphosts/') # Obtain the structure of the remote directory directory_structure = sftp.listdir_attr() # Print data for attr in directory_structure: print attr.filename, attr |
The script above uses the pysftp connection.object to define the host, user, password, and the connection options variables. By default, when connecting to SFTP, the host key is checked using (~/.ssh/known_hosts). However, with the Connection Options (CnOpts), you can specify the correct key file for the connection.
The script above will list every file/directory in the remote server’s directory using Paramiko’s SFTPAttributes objects. This list of SFTP attribute objects is in arbitrary order and does not include unique entries ‘.' and ‘..'.
a. Download a file
To download a remote file, start establishing a connection, then use the “sftp.get” operation. As an example:
import pysftp
Hostname = “hostname” Username = “username” Passsword = “password” with pysftp.Connection(host=Hostname, username=Username, password=Password) as sftp: print “Connection succesfully established … ” # Define the remote file that you want to download remoteFilePath = ‘/var/backups/FILE.txt' # Define the local path where the file will be saved localFilePath = ‘./FILE.txt' sftp.get(remoteFilePath, localFilePath) |
b. Other useful pysftp connection functions?
Aside from pysftp.Connection.get(), and the print attribute, you can also use other functions like:
- pysftp.Connection.put(): To upload a local file to a remote path, use the line: “sftp.put(localFilePath, remoteFilePath)”
- Pysftp.remove. You might want to remove a file from your remote server. To do this, use the following line: sftp.remove(‘/var/example-folder/FILE001.txt')
For more information, check the pysftp cookbook.
5. Use your Python app to access a cloud-based platform
Files.com is a cloud-based secure file-sharing and workflows solution for any size of business. With Files.com, you can exchange files via a highly secured cloud-based platform and perform other tasks for your sensitive files like automation, collaboration, audits, and more.
The Files.com platform allows you to connect via three popular transfer methods: FTP, SFTP, and WebDAV. In addition, you can mount or sync your cloud-based storage like Amazon S3, Azure, Dropbox, etc., in the same platform.
Integrating Files.com with your Python application.
Files.com provides a Software Development Kit (SDK) for Python. This SDK is a set of development tools that provides an easy-to-use Python environment to help developers authenticate users, transfer data with SFTP, create scheduled backups, and more.
The Files.com SDK for Python:
- Allows easy and convenient access to the Files.com API for applications written in Python.
- Provides a set of libraries that are familiar to the Python developers.
- It also comes with tools for files operations included download, upload, list, writing, reading, and more.
- It is recommended to use the Python SDK for all file operations.
To install Files.com SDK for Python:
In addition, you can also use Files.com as an SFTP client. Mount your external SFTP server directly into Files.com like a folder, and make it available via the web interface, Python’s SDK, API, or other inbound SFTP connections.
Start your Files.com free trial.
ExaVault is another file management service that offers a Python library for access to its functions. This service will provide both SFTP and FTPS. You can use the Python library to create your own managed file transfer system and sends files to the cloud storage space that is included with the account.
As well as libraries written in Python, ExaVault has made libraries available for PHP, C#, JavaScript, and Java. These libraries are accessible on the GitHub ExaVault repository.
Although you can access these libraries directly, they won’t do you any good, and neither with the ExaVault API, unless you subscribe to the ExaVault service, which activates the functions that support the libraries.
You can get a 30-day free trial of all of the ExaVault services.
Final Words
To start with SFTP in Python, you’ll need first to get the SFTP’s server information, including IP, credentials (username and password), and the public key. These are variables that you will need to include in your Python script. Then, to implement Python SFTP, you’ll need to install the Pysftp module, which is vital for all your SFTP needs, and which is dependent on two other modules, Paramiko and Cryptography.
In addition, you can also empower your Python applications with a robust third-party SFTP solution. For example, Files.com is a cloud-based file-sharing solution that provides a Python SDK to integrate into your applications.
Αccess SFTP server in Python FAQs
How can I connect to an SFTP server in Python?
You can connect to an SFTP server in Python using the Paramiko library, which provides a secure and convenient way to access SFTP servers in your Python scripts.
What are the steps to access an SFTP server in Python using Paramiko?
To access an SFTP server in Python using Paramiko, you need to:
- Install Paramiko using pip.
- Import the Paramiko library in your script.
- Connect to the SFTP server using the Transport class from Paramiko.
- Authenticate to the SFTP server using a username and password or an RSA key.
- Use the SFTPClient class to interact with the SFTP server and perform file transfers.
How can I download a file from an SFTP server in Python using Paramiko?
To download a file from an SFTP server in Python using Paramiko, you need to:
Connect to the SFTP server using the steps outlined above.
- Use the SFTPClient object's get() method to download a file from the SFTP server.
- Specify the remote file path and the local file path where you want to save the downloaded file.
How can I upload a file to an SFTP server in Python using Paramiko?
To upload a file to an SFTP server in Python using Paramiko, you need to:
Connect to the SFTP server using the steps outlined above.
- Use the SFTPClient object's put() method to upload a file to the SFTP server.
- Specify the local file path and the remote file path where you want to save the uploaded file on the SFTP server.
What are the security considerations when accessing an SFTP server in Python using Paramiko?
When accessing an SFTP server in Python using Paramiko, it's important to take security into consideration. Here are a few things to keep in mind:
- Use secure authentication methods, such as RSA keys, to avoid sending passwords over the network in clear text.
- Verify the authenticity of the SFTP server using its host key.
- Use encryption to protect the data transmitted over the network, such as using SFTP over SSH.