What's the standard way to securely transfer files using Python SFTP?

I’m working on a simple tool in Python SFTP that transfers files to a hard-coded location, with the password also hard-coded. As a Python novice, I initially used ftplib for FTP, which was easy:

import ftplib

info = ('someuser', 'password')  # hard-coded

def putfile(file, site, dir, user=(), verbose=True):
    """
    Upload a file by FTP to a site/directory
    Login hard-coded, binary transfer
    """
    if verbose: print('Uploading', file)
    local = open(file, 'rb')    
    remote = ftplib.FTP(site)   
    remote.login(*user)         
    remote.cwd(dir)
    remote.storbinary('STOR ' + file, local, 1024)
    remote.quit()
    local.close()
    if verbose: print('Upload done.')

if __name__ == '__main__':
    site = 'somewhere.com'  # hard-coded
    dir = './uploads/'  # hard-coded
    import sys, getpass
    putfile(sys.argv[1], site, dir, user=info)

However, I can’t find any library that supports Python SFTP. What’s the standard way to securely transfer files using Python SFTP?

Edit: Thanks to the answers here, I’ve gotten it working with Paramiko, and this was the syntax:

import paramiko

host = "THEHOST.com"  # hard-coded
port = 22
transport = paramiko.Transport((host, port))

password = "THEPASSWORD"  # hard-coded
username = "THEUSERNAME"  # hard-coded
transport.connect(username=username, password=password)

sftp = paramiko.SFTPClient.from_transport(transport)

import sys
path = './THETARGETDIRECTORY/' + sys.argv[1]  # hard-coded
localpath = sys.argv[1]
sftp.put(localpath, path)

sftp.close()
transport.close()
print('Upload done.')

Thanks again!

Hi,

Paramiko is one of the most commonly used libraries for handling SFTP in Python. You can securely transfer files by connecting to the SFTP server and using put() to upload files.

Ensure that you manage the connection properly and handle exceptions for better error handling.

import paramiko

host = "THEHOST.com"  # hard-coded
port = 22
transport = paramiko.Transport((host, port))

password = "THEPASSWORD"  # hard-coded
username = "THEUSERNAME"  # hard-coded
transport.connect(username=username, password=password)

sftp = paramiko.SFTPClient.from_transport(transport)

import sys
path = './THETARGETDIRECTORY/' + sys.argv[1]  # hard-coded
localpath = sys.argv[1]
sftp.put(localpath, path)

sftp.close()
transport.close()
print('Upload done.')

Instead of hardcoding the password, use SSH keys for a more secure connection. By loading the private key file with Paramiko, you can eliminate the need to store the password in your code.

import paramiko

host = "THEHOST.com"  # hard-coded
port = 22
transport = paramiko.Transport((host, port))

private_key_path = "/path/to/private/key"  # Path to your private key
key = paramiko.RSAKey.from_private_key_file(private_key_path)

transport.connect(username="THEUSERNAME", pkey=key)

sftp = paramiko.SFTPClient.from_transport(transport)

import sys
path = './THETARGETDIRECTORY/' + sys.argv[1]  # hard-coded
localpath = sys.argv[1]
sftp.put(localpath, path)

sftp.close()
transport.close()
print('Upload done.')

For a simpler interface, consider using pysftp, a higher-level wrapper around Paramiko that simplifies SFTP operations. It provides methods like put() and get() for easier file handling. import pysftp

with pysftp.Connection('THEHOST.com', username='THEUSERNAME', password='THEPASSWORD') as sftp:
    localpath = sys.argv[1]
    remote_path = './THETARGETDIRECTORY/' + sys.argv[1]  # hard-coded
    sftp.put(localpath, remote_path)
    print('Upload done.')