mag72

How to access SFTP server in Python

How to access SFTP server in Python

Diego Asturias

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

  1. Accessing SFTP in Python
    • What do you need to access an SFTP server in Python?
    • Extracting the SSH key file
  2. The Python required modules
    • Cryptography
    • Paramiko
  3. The pysftp module and an example
    • An Example
  4. Example scripts to list and get
    • Download a file
    • Other useful connection functions
  5. Use your Python app to access a cloud-based platform
    • Integrating Files.com with your Python application
  6. 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.

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.

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:

  1. pysftp.Connection.put(): To upload a local file to a remote path, use the line: “sftp.put(localFilePath, remoteFilePath)”
  2. 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.

Files.com

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:

  1. Install it via PyPi.
  2. You can also retrieve it directly from GitHub.

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.

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.