SFTP in Python? (platform independent) SFTP in Python? (platform independent) python python

SFTP in Python? (platform independent)


Paramiko supports SFTP. I've used it, and I've used Twisted. Both have their place, but you might find it easier to start with Paramiko.


You should check out pysftp https://pypi.python.org/pypi/pysftp it depends on paramiko, but wraps most common use cases to just a few lines of code.

import pysftpimport syspath = './THETARGETDIRECTORY/' + sys.argv[1]    #hard-codedlocalpath = sys.argv[1]host = "THEHOST.com"                    #hard-codedpassword = "THEPASSWORD"                #hard-codedusername = "THEUSERNAME"                #hard-codedwith pysftp.Connection(host, username=username, password=password) as sftp:    sftp.put(localpath, path)print 'Upload done.'


Here is a sample using pysftp and a private key.

import pysftpdef upload_file(file_path):    private_key = "~/.ssh/your-key.pem"  # can use password keyword in Connection instead    srv = pysftp.Connection(host="your-host", username="user-name", private_key=private_key)    srv.chdir('/var/web/public_files/media/uploads')  # change directory on remote server    srv.put(file_path)  # To download a file, replace put with get    srv.close()  # Close connection

pysftp is an easy to use sftp module that utilizes paramiko and pycrypto. It provides a simple interface to sftp.. Other things that you can do with pysftp which are quite useful:

data = srv.listdir()  # Get the directory and file listing in a listsrv.get(file_path)  # Download a file from remote serversrv.execute('pwd') # Execute a command on the server

More commands and about PySFTP here.