Use Python to implement sftp upload and download function code sharing

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.