paramiko라는 라이브러리를 사용하여 sftp 기능을 사용할 수 있다.
import paramiko
# SSH 연결 정보
host = <host ip>
port = <port 번호>
username = <접속할 서버 계정>
password = <접속할 서버의 비밀번호>
# SSH 연결
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(host, port, username, password)
sftp_client = ssh_client.open_sftp()
print("연결")
# SCP 전송
local_file_path = <전송할 파일 경로>
remote_file_path = <도착할 서버 경로>
sftp_client.put(local_file_path, remote_file_path)
print("전송")
# SCP 다운로드
local_file_path = <다운받을 파일 서버 경로>
remote_file_path = <도착할 로컬 파일 경로>
sftp_client.get(remote_file_path, local_file_path)
print("다운로드")
# 파일 삭제
remote_file_path = <삭제할 파일 서버 경로>
sftp_client.remove(remote_file_path)
print("삭제")
# 연결 종료
sftp_client.close()
ssh_client.close()
print("종료")