If you want to upload or copy a file from your local machine to a remote machine securely, you can do so via Secure File Transfer Protocol (SFTP).
This recipe uses a powerful third-party networking library, Paramiko
, to show you an example of file copying by SFTP, as shown in the following command. You can grab the latest code of Paramiko
from GitHub (https://github.com/paramiko/paramiko) or PyPI:
$ pip install paramiko
This recipe takes a few command-line inputs: the remote hostname, server port, source filename, and destination filename. For the sake of simplicity, we can use default or hard-coded values for these input parameters.
In order to connect to the remote host, we need the username and password, which can be obtained from the user from the command line.
Listing 7.2 explains how to copy a file remotely by SFTP, as shown in the following code:
#!/usr/bin/env python # Python Network Programming Cookbook -- Chapter - 7 # This program is optimized for Python 2.7. # It may run on any other version with/without modifications. import argparse import paramiko import getpass SOURCE = '7_2_copy_remote_file_over_sftp.py' DESTINATION ='/tmp/7_2_copy_remote_file_over_sftp.py ' def copy_file(hostname, port, username, password, src, dst): client = paramiko.SSHClient() client.load_system_host_keys() print " Connecting to %s with username=%s... " %(hostname,username) t = paramiko.Transport((hostname, port)) t.connect(username=username,password=password) sftp = paramiko.SFTPClient.from_transport(t) print "Copying file: %s to path: %s" %(SOURCE, DESTINATION) sftp.put(src, dst) sftp.close() t.close() if __name__ == '__main__': parser = argparse.ArgumentParser(description='Remote file copy') parser.add_argument('--host', action="store", dest="host", default='localhost') parser.add_argument('--port', action="store", dest="port", default=22, type=int) parser.add_argument('--src', action="store", dest="src", default=SOURCE) parser.add_argument('--dst', action="store", dest="dst", default=DESTINATION) given_args = parser.parse_args() hostname, port = given_args.host, given_args.port src, dst = given_args.src, given_args.dst username = raw_input("Enter the username:") password = getpass.getpass("Enter password for %s: " %username) copy_file(hostname, port, username, password, src, dst)
If you run this script, you will see an output similar to the following:
$ python 7_2_copy_remote_file_over_sftp.py Enter the username:faruq Enter password for faruq: Connecting to localhost with username=faruq... Copying file: 7_2_copy_remote_file_over_sftp.py to path: /tmp/7_2_copy_remote_file_over_sftp.py
This recipe can take the various inputs for connecting to a remote machine and copying a file over SFTP.
This recipe passes the command-line input to the copy_file()
function. It then creates a SSH client calling the SSHClient
class of paramiko
. The client needs to load the system host keys. It then connects to the remote system, thus creating an instance of the transport
class. The actual SFTP connection object, sftp,
is created by calling the SFTPClient.from_transport()
function of paramiko
. This takes the transport
instance as an input.
After the SFTP connection is ready, the local file is copied over this connection to the remote host using the put()
method.
Finally, it's a good idea to clean up the SFTP connection and underlying objects by calling the close()
method separately on each object.
3.138.69.163