Secure Copy (SCP) is a way to safely transfer files from a local host to a remote host or from a remote host to another remote host. As a software engineer, you must have used SCP at least once in your life.

In this article, we’ll look at how to use SCP with Python. Here, we are going to use the paramiko library.

Let’s understand what is paramiko library first.

Python’s paramiko supports the Secure Copy Protocol (SCP) and the Secure Shell version 2 (SSHv2). Without needing to understand the specifics of the underlying protocol, it enables safe file transfer between hosts in a secure manner.

Following is an example of how to use paramiko to transfer a file between two hosts using python.

import paramiko

ssh = paramiko.SSHClient()

ssh.connect('remote_host', username='user', password='pass')
scp = ssh.open_sftp()
scp.put('local_file', 'remote_file')

# Close the SCP client

# Close the SSH client


  1. We first open an SSH client using the paramiko.SSHClient class and then set the missing host key policy to automatically add the remote host’s SSH key. Please note this only for the testing purpose.
  2. The next step is to establish a connection to the remote host. You’ll need to know the remote host’s name or IP address, as well as the username and password, for this technique to work. Pass these info to the connect and establish a connection with other host.
  3. After the above step, We use the open_sftp method of the SSH client to launch a SCP client. File transfers to and from the remote host are simplified with the help of the paramiko.SFTP class, which is returned by this function.
  4. An SCP client’s put method is utilized here to make the file transfer from the local host to the distant host. ‘local file’ and ‘remote file’ are the two files that can be passed into the put method as parameters.
  5. Finally, we close SCP and SSH clients using close method.

You can visit the following URL to understand more about the paramiko library

Learn more about python

Categorized in:

Tagged in: