Detailed use of Python SFTP

Detailed use of Python SFTP

SFTP (SSH File Transfer Protocol) is a secure file transfer protocol based on the SSH protocol. Python provides the paramiko library to implement the SFTP function. This article will introduce in detail how to use Python and paramiko library for SFTP operation.

Install the paramiko library

First, we need to install the paramiko library. Execute the following commands on the command line:

pip install paramiko

Connect to SFTP server

Next, we need to connect to the SFTP server. First, import the paramiko library and create an SSHClient object:

import paramiko

ssh = paramiko.SSHClient()

Then, we need to set the policy of the SSHClient object to accept the host key of the SFTP server. In a development environment, you can use AutoAddPolicya policy, which automatically accepts all host keys. In a production environment, a more secure strategy should be used:

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

Next, use connectthe method to connect to the SFTP server. The hostname, port, username and password of the server need to be provided:

ssh.connect('hostname', port=22, username='username', password='password')

Create an SFTP session

After successfully connecting to the SFTP server, we need to create an SFTP session. open_sftpCreate a SFTPClient object through the methods of the SSHClient object :

sftp = ssh.open_sftp()

upload files

Now, we can start SFTP operation. First, let's see how to upload files. The methods of the SFTPClient object putcan be used to upload local files to the remote server. You need to provide the local file path and the target path of the remote server:

sftp.put('local_file_path', 'remote_file_path')

download file

In addition to uploading files, we can also download files from remote servers. Use getthe methods of the SFTPClient object to download files from the remote server to the local. You need to provide the remote server file path and the local target path:

sftp.get('remote_file_path', 'local_file_path')

list files and directories

The methods of the SFTPClient object listdircan be used to list the files and directories under the specified directory. The target path needs to be provided:

files = sftp.listdir('directory_path')

Delete Files

Use the method of SFTPClient object removeto delete the specified file. The file path needs to be provided:

sftp.remove('file_path')

Close SFTP session and SSH connection

Once the SFTP operation is done, we need to close the SFTP session and the SSH connection. Call closethe methods of the SFTPClient object and the SSHClient object respectively close:

sftp.close()
ssh.close()

complete example

Here is a complete example demonstrating how to use Python and the paramiko library for SFTP operations:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('hostname', port=22, username='username', password='password')

sftp = ssh.open_sftp()

sftp.put('local_file_path', 'remote_file_path')
sftp.get('remote_file_path', 'local_file_path')

files = sftp.listdir('directory_path')

sftp.remove('file_path')

sftp.close()
ssh.close()

The above is the detailed introduction of SFTP operation using Python and paramiko library. With these methods, you can easily upload, download, list and delete files. Hope this article helps you!

Guess you like

Origin blog.csdn.net/sinat_35773915/article/details/132289840