Below is the code which will guide you on how to make a SFTP(Simple Mail Transfer Protocol) connection using python. In order to do this you first need to install following library to use the inbuild SFTP related functions.
To install the Paramiko module, use the command:
$ pip3 install paramiko
after installing the library use following code:
import paramiko
from io import BytesIO
try:
# Open a transport
sftpTransport = paramiko.Transport(('host_name', PORT_NUMBER))
sftpTransport.connect(None, 'SFTP_USER_NAME', 'SFTP_USER_PASSWORD')
# Open a sftp
sftpClient = paramiko.SFTPClient.from_transport(sftpTransport)
alive = True
print(sftpClient)
for i in sftpClient.listdir():
lstatout=str(sftpClient.lstat(i)).split()[0]
if 'd' in lstatout: print(f'{i} is a directory')
except Exception as e:
print("SFTP: Error while connecting to the SFTP server ", e)
Comments
Post a Comment