Horje
How To Copy Files From One Server To Another in Python

Copying files between servers can seem daunting, but with Python, it becomes a straightforward process. Python provides several libraries to handle file transfers, with paramiko being a popular choice because it allows for SSH connections and file transfers via SFTP (SSH File Transfer Protocol). In this simple guide, we’ll walk through how to use Python and paramiko to transfer a file from one server to another.

Prerequisites

  1. Python installed on your local machine: Ensure Python is set up correctly.
  2. paramiko library: If not already installed, you can install it via pip
pip install paramiko

Step-by-Step Guide to Copy Files Using Python

Step 1: Import paramiko and Set Up SSH Connection

First, import the necessary components from paramiko and establish an SSH connection to the source server where your file is located.

Python
# code
import paramiko

# Establishing an SSH client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Connect to the source server
source_ip = "192.168.1.1"
source_username = "your_username"
source_password = "your_password"
ssh.connect(source_ip, username=source_username, password=source_password)

Step 2: Set Up SFTP Session

Once connected, initiate an SFTP session to manage file transfers.

Python
# code
sftp = ssh.open_sftp()

Step 3: Copy the File

With the SFTP session open, you can now transfer files. Specify the path to the file on the source server and the destination path on the target server.

Python
# code
file_to_transfer = '/path/on/source/file.txt'
destination_path = '/path/on/destination/file.txt'

# The actual file transfer
sftp.get(file_to_transfer, destination_path)

Step 4: Close SFTP Session and SSH Connection

After transferring the files, close the SFTP session and the SSH connection to clean up resources.

Python
# code
sftp.close()
ssh.close()


Example

After applying these steps to copy a file named report.txt from one server to another:

Python
# code
import paramiko

try:
    # Setup SSH connection
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('192.168.1.1', username='admin', password='adminpassword')
    print("SSH connection established.")

    # Setup SFTP session
    sftp = ssh.open_sftp()
    print("SFTP session started.")

    sftp.get('/home/admin/report.txt', '/home/admin/copied_report.txt')
    print("File transfer successful.")

finally:
    if sftp: sftp.close()
    if ssh: ssh.close()
    print("Connections closed.")

Output:

SSH connection established.
SFTP session started.
File transfer successful.
Connections closed.

Conclusion

Using Python and paramiko to copy files between servers is an efficient method that leverages the power of programming to automate and secure file transfers. This method is highly useful for regular backups, data migration, or simply moving data between environments in a controlled manner. With the ability to script and automate these tasks, Python ensures that your data handling processes are both scalable and reliable.




Reffered: https://www.geeksforgeeks.org


Python

Related
How to Add New Line in Dictionary in Python How to Add New Line in Dictionary in Python
Building APIs using FastAPI with Django Building APIs using FastAPI with Django
Python Falcon - Deployment Python Falcon - Deployment
Parsel: How to Extract Text From HTML in Python Parsel: How to Extract Text From HTML in Python
Define Colors in a Figure Using Plotly Graph Objects and Plotly Express Define Colors in a Figure Using Plotly Graph Objects and Plotly Express

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
18