Skip to content

Latest commit

 

History

History
30 lines (23 loc) · 1.02 KB

connecting-to-sftp.md

File metadata and controls

30 lines (23 loc) · 1.02 KB

Connecting to SFTP via Paramiko

Using Paramiko to connect to an SFTP is pretty easy after some digging around. This is how I have done it via the use of Transport. Once connected you can simply use the SFTP client to do what is needed.

Below I connect to the SFTP of my choice, change to a certain directory then upload a file to that directory before closing the connection.

def sftp_test(change_of_dir: str, localpath: str, remotepath: str):
    try:
        transport = paramiko.Transport(host, 22)
        transport.start_client()
        transport.auth_password(config['username'], config['password'])

        sftp = paramiko.SFTPClient.from_transport(transport)

        sftp.chdir(change_of_dir)
        sftp.put(localpath, remotepath)

        sftp.close()

    except Exception as err:
        print(f"Could not connect SFTP client: {err}")
        raise err