Python Secure File Transfer with scp & sftp
Transferring files securely between local and remote machines is achieved using `scp` (Secure Copy Protocol) and `sftp` (Secure File Transfer Protocol). Both run on top of SSH on port 22, guaranteeing encryption in flight.
"If SSH is an encrypted telephone call with a remote server, `scp` is an encrypted armored courier vehicle transporting a package across that same tunnel."
Deep Dive: How It Works
scp Syntax: `scp [options] [user@]source_host:source_path [user@]dest_host:dest_path`.
Flags: `-r` (recursive directory transfer), `-P <port>` (custom SSH port; note capital P), `-i <key>` (path to private identity key), `-C` (enable gzip compression on the wire).
sftp Batching: `sftp -b batchfile.txt user@host` allows non-interactive scripted multi-file transfers.
Syntax Blueprint
scp file.txt user@remote.com:/var/www/ # Local to Remote scp user@remote.com:/var/log/app.log ./logs/ # Remote to Local scp -r -P 2222 ./dist/ user@remote.com:/app/ # Recursive with custom port
Use scp for simple single-command transfers and sftp for interactive browsing.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using lowercase `-p` with scp to specify a port.Why it happens: In scp, lowercase `-p` stands for "preserve file modification times and modes".
How to fix: Always use uppercase `-P` for network port specification with scp.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Verify SCP Syntax Understanding
Echo "scp -r -P 2222 build/ user@server:/var/www" to demonstrate scp command structure.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.