HOWTO · Python
SCP in Python
This tutorial explains what is SCP and how to use it in Python.
On this page
Use the scp package to send files to and receive files from an SSH server in Python. The examples below use Paramiko for the SSH connection, then create an SCPClient from that connection’s transport. You need a reachable SSH server and valid authentication details; replace the sample paths and host settings with values for your environment.
Install the scp Package
Install the package in the Python environment that will run your script with python -m pip install scp. The scp package uses paramiko for SSH transport, so installing it also makes the Paramiko classes available. Run the command in a virtual environment when you want to keep project dependencies separate.
Create an SSH Connection and SCPClient
Import SSHClient from paramiko and SCPClient from scp. Create an SSH client, load the host keys trusted by your environment with ssh.load_system_host_keys(), and connect to your server with ssh.connect("server.example", username="your-user", key_filename="/path/to/private-key"). These are placeholders: use a real host and an authentication method configured for your server. Then create the transfer client with scp = SCPClient(ssh.get_transport()). Keep both client objects available until transfers finish.
Upload and Download Files
Call scp.put(local_path, remote_path) to upload a local file. For example, scp.put("report.csv", "/srv/incoming/report.csv") reads report.csv on your computer and writes it to the remote destination. Call scp.get(remote_path, local_path) to download a remote file; scp.get("/srv/outgoing/result.csv", "result.csv") saves it in the current local directory. Use explicit paths when the script’s working directory may vary. Confirm that the SSH account can read the source and write to the destination.
Transfer Directories
Use put() with recursive=True when the local source is a directory. For example, scp.put("daily-reports", recursive=True, remote_path="/srv/archive") transfers the directory tree beneath the remote destination. The remote_path argument names the destination directory on the server. Ensure that it exists or that the account can create it, and check the resulting remote layout on your server before relying on a scheduled transfer.
Report Transfer Progress
Pass a callback to the SCPClient constructor to report bytes transferred. A progress callback receives the filename, total size, and bytes sent. progress4 receives those same values plus peer information. Define the callback yourself; these names identify callback arguments accepted by SCPClient, not functions to import. Choose one callback for a client. The small example below formats a percentage from the callback values; its final line invokes the function with sample values.
Run this callback example:
def progress(filename, size, sent):
if size:
print(f"{filename}: {sent / size:.1%}")
progress("report.csv", 100, 25)
report.csv: 25.0%
To use it for a transfer, pass it as SCPClient(ssh.get_transport(), progress=progress) and then call put() or get() on that client. The callback reports transfer updates; output formatting and logging are up to your application. The example’s size check avoids dividing by zero when formatting a percentage.
Close the Connection and Handle Errors
Close the SCP client and the SSH client when the transfer is done: call scp.close() and then ssh.close(). Put transfer operations inside try and place both close calls in a finally block so cleanup runs when control leaves the transfer section. If setup fails before scp is created, close the SSH client that was created. Let your application decide how to report or retry connection and file errors; check the host, authentication, and local and remote paths rather than assuming a transfer succeeded. These steps provide a practical Python workflow for sending and receiving files over SSH.