In Python, you can use sftp in the paramiko module to log on to the remote host for upload and download. Next, we will introduce Python's function of using sftp for upload and download. For more information, see using sftp in the paramiko module in Python to log on to the remote host. Next, I will introduce Python's function of uploading and downloading using sftp. For more information, see
1. function implementation
Upload and download files or directories based on input parameters
The local parameter must be of the same type as the remote parameter. the file ends with the file name and the directory ends with the \ parameter.
The local and remote directories for upload and download must exist.
Exception capture
2. code implementation
#! /Usr/bin/python # coding = utf-8import paramikoimport osdef sftp_upload (host, port, username, password, local, remote): sf = paramiko. transport (host, port) sf. connect (username = username, password = password) sftp = paramiko. SFTPClient. from_transport (sf) try: if OS. path. isdir (local): # determine whether the local parameter is a directory or a file for f in OS. listdir (local): # traverse the local directory sftp. put (OS. path. join (local + f), OS. path. join (remote + f) # upload the file else in the directory: sftp. put (local, remote) # upload file failed t Exception, e: print ('upload exception: ', e) sf. close () def sftp_download (host, port, username, password, local, remote): sf = paramiko. transport (host, port) sf. connect (username = username, password = password) sftp = paramiko. SFTPClient. from_transport (sf) try: if OS. path. isdir (local): # determine whether the local parameter is a directory or a file for f in sftp. listdir (remote): # traverse the remote directory sftp. get (OS. path. join (remote + f), OS. path. join (local + f) # Download the else: sftp file in the directory. get (remote, local) # download file failed t Exception, e: print ('Download exception: ', e) sf. close () if name = 'main': host = '2017. 168.1.2 '# host port = 22 # port username = 'root' # username password = '000000' # password local = 'F: \ sftptest \' # local file or directory, consistent with remote. The current format is windows directory. the double-slash remote = '/opt/tianpy5/python/test/' # is used in the middle of the window Directory. the remote file or directory is consistent with the local directory, sftp_upload (host, port, username, password, local, remote) in linux directory format # upload # sftp_download (host, port, username, password, local, remote) # Download
3. Summary
The above code uploads and downloads files and directories. you can upload and download files separately, or upload and download files in multiple directories. basically, you have implemented the required functions, however, it remains to be improved in the case that the directory does not exist and that the files are uploaded and downloaded to multiple hosts.
The above describes how to use Python to implement sftp upload and download functions. For more information, see other related articles in the first PHP community!