SFTP is an SSH-based file Transfer protocol that is the most common way to transfer files to Linux on Windows (for example, securefx,xftp).
Under Python, Paramiko implements SFTP, allowing you to easily implement file transfer functionality in your code.
Official website is here: http://www.lag.net/paramiko/
You can use Easy_install Paramiko to install.
The following is a sample code that uses Paramiko to transfer files
10 |
t = paramiko.Transport((hostname, port)) |
11 |
t.connect(username = username, password = password) |
13 |
sftp = paramiko.SFTPClient.from_transport(t) |
15 |
sftp.put( "/home/***/py_test/from/1.txt" , "/home/***/py_test/to/1.txt" ) |
16 |
sftp.get( "/home/***/py_test/to/2.txt" , "/home/***/py_test/from/2.txt" ) |
An sftp put means that a local file is transferred to a remote machine, and a get indicates that the remote file is passed to the local machine. Put and get need to explicitly write out the file name, unlike the SCP that can be omitted.
It is important to note that the put and get functions of SFTP only support a single file, if you need to transfer the entire directory, you need to manually use Sftp.mkdir to create a directory, and then traverse the entire folder, and use the put or get function for each file.
Using Paramiko to implement SFTP