
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to copy a file to a remote server in Python using SCP or SSH?
The easiest way to copy files from one server to another over ssh is to use the scp command. For calling scp you'd need the subprocess module.
example
import subprocess p = subprocess.Popen(["scp", "my_file.txt", "username@server:path"]) sts = os.waitpid(p.pid, 0)
You need the waitpid call to wait for the copying to complete.
Another solution is to open a ssh connection and use the scp module.
example
from paramiko import SSHClient from scp import SCPClient ssh = SSHClient() ssh.load_system_host_keys() ssh.connect('user@server:path') with SCPClient(ssh.get_transport()) as scp: scp.put('my_file.txt', 'my_file.txt') # Copy my_file.txt to the server
- Related Articles
- How to Use SFTP to Secure File Transfer with a Remote Server
- How to copy files from one server to another using Python?
- How to Copy Odd Lines of Text File to Another File using Python
- How to copy tables or databases from one MySQL server to another MySQL server?
- How to clone or copy a list in Python?
- How to copy a file, group of files, or directory in Linux?
- How do I copy a file in python?
- How to build JCo server without using a Properties file in SAP?
- How to copy a table in MySQL using Python?
- How to check if a file exists or not using Python?
- How do I copy a binary file in Python?
- How to Copy a File to Multiple Directories in Linux?
- Running a Shell Script on a Remote Machine Through SSH
- How to copy files to a new directory using Python?
- How to resume a partially transferred file over ssh on Linux?

Advertisements