The various application requires transferring and uploading files from one system to the other. It is done by the File Transfer Protocol. It is a communication protocol that helps to transfer files from a server to a client over the computer network. FTP works on top of Client-Server architecture and acts as a standard means to separate controlling & data connectivity for delivering data. In this article, you will learn about the concept of SFTP and how to implement it in a Python program.
What is SFTP?
Secure File Transfer Protocol (SFTP) is a protocol that allows transferring files of different sizes over the network. It creates a secured connection using the Secure Shell (SSH) and is built on top of File Transfer Protocol (FTP) with various security components. Internet Engineering Task Force (IETF) designed this SFTP using the SSH containing cryptographic algorithm for better security postures while transferring data. SFTP-based data transferring becomes handy and easy to program where data traversal from one system to another needs adequate protection.
Using SFTP in a Python Program:
To run the file transferring program over a secure shell, you need to use the pysftp module in your Python program. This module is wrapped around paramiko and utilizes pycrypto libraries to perform the secure transferring of data. Pysftp is easy to implement. There are various methods of pysftp that helps in increasing the productivity of a programmer by encapsulating different higher functions.
To use it in our Python program, you have to install it.
python -m pip install pysftp
Note: One thing to keep in mind that the pysftp interface doesn't expose much of its Paramiko features. Most of the Paramiko implementations are in abstract form in single methods. Pysftp focuses more on implementing the high-level functionalities on top of Paramiko.
Features of pysftp:
- It has a simple interface.
- It can easily and automatically handle RSA and DSS private key files.
- It supports private key-based file encryption.
- Programmers can enable and disable the login features while using it.
Listing all the remote directories:
Listing the directories and their absolute path is a necessary phase of data transferring using SFTP. To connect to our FTP server, you have to first import the pysftp module by specifying the credentials such as username, password, and server. Let us now create a program, where we can fetch & print all the directories and files one after another in a list fashion.
import pysftp serv_details = pysftp.Connection(host = "ur_ftp_servername", username = "anyName", password = "put your Password") # List all the directories and its associated files for transferring dat = serv_details.listdir() serv_details.close() # Displaying all the directories and files in a listed fashion for df in dat: print (df)
First, we will import the pysftp module. Next, we have placed the connection by putting the host/server, username, and password. Next, we have to list the directory using the listdir() method and store it in another variable ‘dat’. This dat object will hold all the collection of directories listed from the remote server.
We can now end the connection using the close() method. Now, we execute a for loop to grab all the directories from the dat and display them.
Upload & Download a File from a Remote Server / location:
From the previous example, you have seen how to import the pysftp module by implementing the credentials like server name, username, and password. Also, we have been able to fetch the directory path and listed it. Now, it is time to download and upload the file from a remote server using the get() and put() methods.
import pysftp import sys remote_data = sys.argv serv_details = pysftp.Connection(host = "yourdomainName.com", username = "anyName", password = "put your Password") # This method will download your file from the specified remote server serv_details.get(remote_data) # This method will upload your file from the specified remote server serv_details.put(remote_file) # Connection closed serv_details.close()
First, we will import the pysftp module and the sys module. Then, we will use sys.argv to represent the first command-line argument (in the form of a string) to supply the value stored in the remote_data variable. This will allow you put the path and filename that you want to upload or donload at the time of program run. Next, we have placed the connection by putting the host/server, username, and password.
Now, you will use the get() method to securely download the file from the specified remote server. Also, if you wish to securely upload any file to the remote server, the put() method will make it happen for you. Once we are done with the transferring of file to and fro, we will end the connection with close() method.
Another Way of writing the program:
import pysftp hostName = "myserverdomain.in" userName = "karlos" pswd = "6@ur4v" with pysftp.Connection(host = hostName username = userName password=pswd) as serv_details: print ("Connection established ... ") remoteFileLoc = '/karlos/pyexample-db/DATABASE.txt' localFileLoc = './DATABASE.txt' serv_details.get(remoteFileLoc, localFileLoc ) serv_details.put(remoteFileLoc, localFileLoc ) serv_details.close()
Deleting files transferred using SFTP:
If you wish to remove any file from the remote server you have connected to earlier, you can do it using the pysftp module itself. The remove() method helps in removing or deleting the file that you have to specify by providing the absolute path as the argument in this method.
import pysftp hostName = "myserverdomain.in" userName = "karlos" pswd = "6@ur4v" with pysftp.Connection(host = hostName username = userName password=pswd) as serv_details: print ("Connection established ... ") # Deleting from the given absolute path serv_details.remove('/karlos/pyexample-db/DATABASE.txt')
PySFTP has a broad spectrum of methods and uses that you can learn by going through its documentation. But mentioned three are the most prominent tasks that Python developers use for transferring (upload and download) files through python code.