SFTP

The SFTP class allows you to interact with SFTP services.

It uses the Paramiko SFTP library under the hood.

class parsons.SFTP(host, username, password, port=22, rsa_private_key_file=None)[source]

Instantiate SFTP Class

Args:
host: str

The host name

username: str

The user name

password: str

The password

rsa_private_key_file str

Absolute path to a private RSA key used to authenticate stfp connection

port: int

Specify if different than the standard port 22

Returns:

SFTP Class

create_connection()[source]

Create an SFTP connection. You can then utilize this in a with block and it will close the connection when it is out of scope. You should use this when you wish to batch multiple methods using a single connection.

import SFTP

sftp = SFTP()
connection = sftp.create_connection()

with connection as conn:
    sftp.make_directory('my_dir', connection=conn)
    sftp.put_file('my_csv.csv', connection=conn)
Returns:

SFTP Connection object

list_directory(remote_path='.', connection=None)[source]

List the contents of a directory

Args:
remote_path: str

The remote path of the directory

connection: obj

An SFTP connection object

Returns:

list

make_directory(remote_path, connection=None)[source]

Makes a new directory on the SFTP server

Args:
remote_path: str

The remote path of the directory

connection: obj

An SFTP connection object

remove_directory(remote_path, connection=None)[source]

Remove a directory from the SFTP server

Args:
remote_path: str

The remote path of the directory

connection: obj

An SFTP connection object

get_file(remote_path, local_path=None, connection=None)[source]

Download a file from the SFTP server

Args:
remote_path: str

The remote path of the file to download

local_path: str

The local path where the file will be downloaded. If not specified, a temporary file will be created and returned, and that file will be removed automatically when the script is done running.

connection: obj

An SFTP connection object

Returns:
str

The path of the local file

get_table(remote_path, connection=None)[source]

Download a csv from the server and convert into a Parsons table.

The file may be compressed with gzip, or zip, but may not contain multiple files in the archive.

Args:
remote_path: str

The remote path of the file to download

connection: obj

An SFTP connection object

Returns:
Parsons Table

See Parsons Table for output options.

put_file(local_path, remote_path, connection=None)[source]

Put a file on the SFTP server Args:

local_path: str

The local path of the source file

remote_path: str

The remote path of the new file

connection: obj

An SFTP connection object

remove_file(remote_path, connection=None)[source]

Delete a file on the SFTP server

Args:
remote_path: str

The remote path of the file

connection: obj

An SFTP connection object

get_file_size(remote_path, connection=None)[source]

Get the size of a file in MB on the SFTP server. The file is not downloaded locally.

Args:
remote_path: str

The remote path of the file

connection: obj

An SFTP connection object

Returns:
int

The file size in MB.