Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to copy a file to a remote server in Python using SCP or SSH?
When we want to transfer files from our local system to a remote server securely, Python provides several ways to do file transfer using the Paramiko and SCP libraries. These libraries support SSH-based file transfer, which is secure and reliable.
Installing Required Libraries
Before we start with file transfer, we need to install the required libraries ?
pip install paramiko scp
Understanding SCP and SSH
Paramiko is a third-party Python library that provides SSH and SFTP functionality using the SSHv2 protocol. SCP stands for Secure Copy Protocol, which copies files between computers over a secure SSH connection. SSH stands for Secure Shell, providing a secure channel over an unsecured network.
Method 1: Using Paramiko with SCP
This approach combines SCP and SSH tools with Paramiko to perform secure file transfers ?
import paramiko
from scp import SCPClient
# Remote server credentials
hostname = '192.168.1.100'
port = 22
username = 'testuser'
password = 'testpass'
# File paths
local_file_path = 'sample.txt'
remote_path = '/home/testuser/sample.txt'
# Create a sample file to transfer
with open(local_file_path, 'w') as f:
f.write("Hello from local machine!")
# Create SSH client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# Connect to the server
ssh.connect(hostname=hostname, port=port, username=username, password=password)
print("Connected to remote server")
# SCP file transfer
with SCPClient(ssh.get_transport()) as scp:
scp.put(local_file_path, remote_path)
print("File copied successfully using SCP!")
except Exception as e:
print(f"Error: {e}")
finally:
ssh.close()
Connected to remote server File copied successfully using SCP!
Method 2: Using SFTP (SSH File Transfer Protocol)
SFTP is another secure method for file transfer that works over SSH ?
import paramiko
# Remote server credentials
hostname = '192.168.1.100'
port = 22
username = 'testuser'
password = 'testpass'
# File paths
local_file_path = 'document.txt'
remote_path = '/home/testuser/document.txt'
# Create a sample file
with open(local_file_path, 'w') as f:
f.write("This file was transferred via SFTP!")
# Create SSH client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# Connect to the server
ssh.connect(hostname=hostname, port=port, username=username, password=password)
# Create SFTP client
sftp = ssh.open_sftp()
# Transfer file
sftp.put(local_file_path, remote_path)
print("File copied successfully using SFTP!")
# Close SFTP connection
sftp.close()
except Exception as e:
print(f"Error: {e}")
finally:
ssh.close()
File copied successfully using SFTP!
Method 3: Using SSH Key Authentication
For enhanced security, use SSH key authentication instead of passwords ?
import paramiko
from scp import SCPClient
# Remote server credentials
hostname = '192.168.1.100'
port = 22
username = 'testuser'
private_key_path = '/path/to/private/key' # Path to your private key
# File paths
local_file_path = 'secure_file.txt'
remote_path = '/home/testuser/secure_file.txt'
# Create a sample file
with open(local_file_path, 'w') as f:
f.write("Secure file transfer using SSH keys!")
# Load private key
try:
private_key = paramiko.RSAKey.from_private_key_file(private_key_path)
except:
# For demo purposes, we'll use password authentication
private_key = None
# Create SSH client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
if private_key:
ssh.connect(hostname=hostname, port=port, username=username, pkey=private_key)
else:
# Fallback to password for demo
ssh.connect(hostname=hostname, port=port, username=username, password='testpass')
# Transfer using SCP
with SCPClient(ssh.get_transport()) as scp:
scp.put(local_file_path, remote_path)
print("File copied successfully using SSH key authentication!")
except Exception as e:
print(f"Error: {e}")
finally:
ssh.close()
File copied successfully using SSH key authentication!
Comparison
| Method | Security | Use Case | Speed |
|---|---|---|---|
| SCP with Paramiko | High | Simple file transfers | Fast |
| SFTP | High | Advanced file operations | Moderate |
| SSH Key Auth | Highest | Production environments | Fast |
Error Handling Best Practices
Always implement proper error handling for network operations ?
import paramiko
from scp import SCPClient
import os
def copy_file_to_remote(hostname, username, password, local_path, remote_path):
"""Copy file to remote server with proper error handling"""
# Check if local file exists
if not os.path.exists(local_path):
print(f"Local file {local_path} not found!")
return False
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(hostname=hostname, username=username, password=password, timeout=10)
with SCPClient(ssh.get_transport()) as scp:
scp.put(local_path, remote_path)
print(f"Successfully copied {local_path} to {hostname}:{remote_path}")
return True
except paramiko.AuthenticationException:
print("Authentication failed!")
except paramiko.SSHException as e:
print(f"SSH connection error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
finally:
ssh.close()
return False
# Example usage
success = copy_file_to_remote(
hostname='192.168.1.100',
username='testuser',
password='testpass',
local_path='test.txt',
remote_path='/home/testuser/test.txt'
)
if success:
print("File transfer completed successfully!")
Successfully copied test.txt to 192.168.1.100:/home/testuser/test.txt File transfer completed successfully!
Conclusion
Python provides multiple secure methods for copying files to remote servers using Paramiko and SCP libraries. Use SCP for simple transfers, SFTP for advanced operations, and SSH key authentication for enhanced security in production environments.
