How to copy files from one server to another using Python?



Transferring files from one server to another is a most common task in system administration, DevOps and development workflows. The tools such as scp and rsync are used for the file transfer.

Python provides different methods and tools to transfer the files from one server to another using a third party libraries such as paramiko and SCPClient.

Using Paramiko and SCP for Secure Transfers

When we want to copy files securely from one server to another using Python, we can use the libraries Paramiko and SCPClient together. Paramiko is a Python library that helps to implement SSHv2 protocol.

This is used to establish SSH connections to remote servers programmatically, and SCPClient builds on Paramiko to enable file transfer using the SCP (Secure Copy Protocol) by making it ideal for securely moving files over the network.

Following are the steps involved to copy files from one server to another using the libraries Paramiko and SCP -

Installing required libraries

First, we need to install the two libraries by using the below command, if they are not installed before in our system -

pip install paramiko scp

Establishing an SSH Connection to Upload a File

Once all the required libraries are installed then we need to create a script that connects to a remote server and uploads the given file. Following is the script to establish an SSH Connection to upload a file -

from paramiko import SSHClient
from scp import SCPClient

# Initialize the SSH client
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect(hostname='192.168.1.10', username='admin', password='mypassword')

# Initialize SCP and upload the file
with SCPClient(ssh.get_transport()) as scp:
   scp.put('example.txt', './home/admin/example.txt')

print("File uploaded successfully.")

Downloading a File from the Remote Server

We can download a file from the remote server by using the same above script used for establishing an SSH connection, butweneed to modify the direction of the transfer as follows -

from paramiko import SSHClient
from scp import SCPClient

# Initialize the SSH client
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect(hostname='192.168.1.10', username='admin', password='mypassword')

# Initialize SCP and upload the file
with SCPClient(ssh.get_transport()) as scp:
   scp.get('./home/admin/example.txt', 'report.csv')

print("File uploaded successfully.")
Updated on: 2025-06-20T19:31:03+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements