SFTP Client

Purpose

This section will go through an example of using the TransPort as a SFTP Client to retrieve a file via SFTP to some SFTP Server.

import ftplib_d
from ftplib_d import FTP_TLS

def SFTPClient():
    ftps = FTP_TLS('IP_ADDRESS_OF_SERVER')
    ftps.login('username', 'password')
    ftps.prot_p()    
    ftps.retrlines('LIST')
    ftps.retrbinary('RETR filename.txt', open('filename.txt', 'wb').write)
    ftps.quit()

SFTPClient()

Note “ftplib_d” is the Python 2.7 "ftplib" Python file. Python 2.6 on the TransPort does not support SFTP on its own. This file must be loaded on the TransPort before this script will run properly.

Code Breakdown

import ftplib_d
from ftplib_d import FTP_TLS

Here the code is importing the “ftplib_d” module and from that module, the function “FTP_TLS”. As mentioned in the note above, “ftplib_d” is just the Python 2.7 version of the “ftplib” library that is already on the TransPort, but the Python implementation of the TransPort (Version 2.6) does not support this function, hence the need for the 2.7 library instead. This file can be obtained from www.Python.org and just rename it to something like “ftplib_d” to call it in your code.

def SFTPClient():
    ftps = FTP_TLS('IP_ADDRESS_OF_SERVER')
    ftps.login('username', 'password')
    ftps.prot_p()
    ftps.retrlines('LIST')
    ftps.retrbinary('RETR filename.txt', open('filename.txt', 'wb').write)
    ftps.quit()
  1. First, we define the program itself. (def SFTPClient():)
  2. A variable called “ftps” is created with the values to SFTP into the Server IP address. (ftps = FTP_TLS('IP_ADDRESS_OF_SERVER'))
  3. Using the ‘ftps’ variable, perform a login of the server with the given username and password. (ftp.login('username', 'password')).
  4. Using the ‘ftps’ variable, switch the mode into a secure communications mode. (ftps.prot_p())
  5. Using the ‘ftps’ variable, “LIST” the contents of the FTP server. (ftp.retrlines('LIST'))
  6. Using the ‘ftps’ variable, return the file name we are asking for, open the file, and write it to the TransPort. (ftps.retrbinary('RETR filename.txt', open('filename.txt', 'wb').write))
  7. The last line quits the FTP operation. (ftps.quit())

 

  SFTPClient()

This last portion of the code tells it to run the definition that was previously created.