It is not uncommon for organizations to use a variety of internal information management tools. The data used in these applications is often segregated and, at times, not easily accessible through a direct API connection.
In cases such as this, it is often possible to work around data sharing limitations by pushing information to an intermediary server prior to loading the data into another application. This is where the Secure Transfer Protocol or SFTP comes in.
As the name implies, SFTP allows a client to establish a secure (encrypted) connection with a server to share data or other information. Often, SFTP server connections are managed using applications like cyberduck (for all you mac users), putty, or WinSCP. Although these tools are optimal for connecting to and examining data on a server or conducting configuration on an ad hoc basis, they are less ideal for automating the transfer of information from one server to another.
Say, for example, you were running an application that relies on feed data from a website lacking the ability to directly post files to another server through SFTP. You could simply download the file from the website and manually post to the target server using aforementioned tools. However, why not instead write a simple python script to pull the data from the website and post automatically to the target SFTP, thus freeing you from the monotony of the task and, most importantly, the human errors of manual manipulation?
I asked myself the same question several weeks ago and was pleasantly surprised how easy the SFTP connection and file posting process is using the pysftp package.
Reference the code snippet below for how the package is used in practice. In summary, the function works with pysftp to establish the connection to the server, define the remote path of the file you're delivering, and the local path of the file you want to post. From there pysftp can post the desired file using the 'put' method.
import os import pysftp as sftp #ask the user for a username username = input('what is the server username?: ') #ask the user for a password server_pw = getpass.getpass('What is the password to the SFTP?:') def sftpPosting(pw,username): #Step 1: establish the connection s=sftp.Connection(host='EXAMPLE-SERVER',username=username,password=pw) #Step 2: define the remote path (drop location on the server) remotePath='/examplefilepath' #Step 3: define the local path (where the file you want to upload is) localPath = '/examplefilepath' #Step 4: put the file on the server s.put(localPath,remotePath) #Step 5: close the connection s.close() #run the function sftpPosting(server_pw,username) #provide notification the file sucessfully executed print('File is now loaded onto the target server!')
Feel free to use the code snippet to get familiar with the use of pysftp. The link to the package website is also listed in the Reference section if you'd like to learn more. Please let me know if you have any questions, recommendations, or code suggestions by starting the conversation below!
Thanks for reading. Til' next time.
- Info on SFTP - https://www.2brightsparks.com/resources/articles/ssh-file-transfer-protocol-explained.pdf
- Source Site for pysftp - https://pysftp.readthedocs.io/en/release_0.2.9/