Python Network Programming SFTP
Python Network Programming with SFTP
SFTP, also known as the SSH File Transfer Protocol, is a network protocol that provides file access, file transfer, and file management over any reliable data stream. The program operates over a secure channel, such as SSH, where the server has authenticated the client and the client user’s identity is available to the protocol.
pysftp is a simple interface to SFTP. This module provides high-level abstractions and task-based routines to handle SFTP needs. Therefore, we install the module into our Python environment using the following command.
pip install pysftp
Example
In the following example, we use SFTP to log in to a remote server and then retrieve and place some files in that directory.
<br>import pysftp
with pysftp.Connection('hostname', username='me', password='secret') as sftp:
with sftp.cd('/allcode'): # temporarily chdir to allcode
sftp.put('/pycode/filename') # upload file to allcode/pycode on the remote
sftp.get('remote_file') # get a remote file
When we run the above code, we can see a list of files in the allcode directory and also put and get files there.