Python SFTP File Transfers
open_sftp() on a connected client gives you a full filesystem API over the same connection - put, get, stat, listdir, and an open() that reads a remote file with no local copy. The parts worth knowing are the ones that are not obvious: confirm=True is a size check and not a checksum, an upload is only atomic if you rename it, there is no walk or rmtree, and half the errors arrive with no errno at all.
Remote Hosts and CI/CD Guide 33 of 39 Intermediate
- Control nodeUbuntu 26.04 LTS
- Managed hostsRHEL 10.0
- Python3.14.4
- requests2.34.2
- TimeAbout 20 min
paramiko 5.0.0. posix_rename needs the server to support the posix-rename@openssh.com extension, which OpenSSH has had for many years; the fallback matters for other servers.
- paramiko5.0.0
- pytest9.1.1
- PyYAML6.0.3
- boto3 / botocore1.43.78
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Control node - every script in this path runs here | 2 Core | 4 GB | 50 GB |
| RHCSA-A01 | 192.168.0.31 | RHEL 10.0 | Managed host - reached over SSH from the control node | 2 Core | 4 GB | 50 GB |
| RHCSA-B01 | 192.168.0.33 | RHEL 10.0 | Second managed host - so an inventory has more than one row | 2 Core | 4 GB | 50 GB |
This guide includes
Use this for file transfer over the connection you already have. This matters because a partial upload under the final name is indistinguishable from a complete one - so the destination name matters more than the transfer call does.
- putting a file and getting it back, with a digest at both ends
- finding what
confirm=is actually doing - uploading to a temporary name and renaming, so a partial file is never taken for a whole one
- listing, statting and reading in place, and walking a remote tree SFTP will not walk for you
- reading the errors, including the ones with no errno, and adding a progress callback
Before you start
- guide 31 - the client this opens a channel on.
- guide 15 - the same atomic-write argument, locally.
- guide 10 - and why posixpath rather than os.path here.
-
A file up, and the same file back
-
What confirm= is actually doing
-
A partial upload, and why the destination name matters
-
Listing, statting and reading in place
-
Walking a remote tree, which SFTP does not do for you
-
The errors, and the ones with no errno at all
-
A progress callback
-
And the same job with scp, for comparison
-
Cleaning up, on both ends