{"library":"scp","code":"import os\nfrom paramiko import SSHClient, AutoAddPolicy\nfrom scp import SCPClient\n\n# Configuration from environment variables for security\nHOSTNAME = os.environ.get('SCP_HOSTNAME', 'your_remote_host')\nUSERNAME = os.environ.get('SCP_USERNAME', 'your_username')\nPASSWORD = os.environ.get('SCP_PASSWORD', '') # Use SSH keys in production\n\nlocal_file = 'local_test_file.txt'\nremote_path = '/tmp/remote_test_file.txt'\n\n# Create a dummy local file for the example\nwith open(local_file, 'w') as f:\n    f.write('Hello from scp.py!')\n\nssh = SSHClient()\n# Set policy to auto-add host keys for demo. In production, use ssh.load_system_host_keys() or HostKeys().add().\nssh.set_missing_host_key_policy(AutoAddPolicy()) \n\ntry:\n    # Connect to the remote server\n    ssh.connect(hostname=HOSTNAME, username=USERNAME, password=PASSWORD, port=22)\n    print(f\"Connected to {HOSTNAME}\")\n\n    # SCPCLient takes a paramiko transport as an argument\n    with SCPClient(ssh.get_transport()) as scp:\n        # Upload a file\n        scp.put(local_file, remote_path)\n        print(f\"Uploaded '{local_file}' to '{remote_path}'\")\n\n        # Download the file back to verify\n        downloaded_file = 'downloaded_test_file.txt'\n        scp.get(remote_path, downloaded_file)\n        print(f\"Downloaded '{remote_path}' to '{downloaded_file}'\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\nfinally:\n    ssh.close()\n    print(\"SSH connection closed.\")\n\n# Clean up dummy files\nimport os\nif os.path.exists(local_file):\n    os.remove(local_file)\nif os.path.exists(downloaded_file):\n    os.remove(downloaded_file)","lang":"python","description":"This quickstart demonstrates how to establish an SSH connection using `paramiko.SSHClient` and then use `scp.SCPClient` to upload and download a file. It includes important considerations for host key policy and proper connection closure.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}