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 AutoAddPolicy
a 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 connect
the 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_sftp
Create 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 put
can 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 get
the 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 listdir
can 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 remove
to 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 close
the 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!