connect to sftp python code example
Example 1: python connect sftp with key
import pysftp
def upload_file(file_path):
private_key = "~/.ssh/your-key.pem"
srv = pysftp.Connection(host="your-host", username="user-name", private_key=private_key)
srv.chdir('/var/web/public_files/media/uploads')
srv.put(file_path)
srv.close()
Example 2: python connect sftp
import pysftp
import sys
path = './THETARGETDIRECTORY/' + sys.argv[1]
localpath = sys.argv[1]
host = "THEHOST.com"
password = "THEPASSWORD"
username = "THEUSERNAME"
with pysftp.Connection(host, username=username, password=password) as sftp:
sftp.put(localpath, path)
print 'Upload done.'