RedSSH.sftp

class redssh.sftp.RedSFTP(ssh_session)[source]

Bases: object

This will only interact with the remote server as the user you logged in as, not the current user you are running commands as.

Set self.ignore_existing_dirs to False to make redssh.sftp.RedSFTP.mkdir not ignore already existing directories.

close(file_obj)[source]

Closes a file object over SFTP on the remote server. It is a good idea to delete the file_obj after calling this.

Parameters

file_obj (ssh2.sftp.SFTPHandle) – ssh2.sftp.SFTPHandle to interact with.

Returns

None

fsetstat(file_obj, attrs)[source]

Set file stat attributes for a ssh2.sftp.SFTPHandle object.

Parameters
  • file_obj (ssh2.sftp.SFTPHandle) – ssh2.sftp.SFTPHandle of the file on the remote server.

  • attrs (ssh2.sftp.SFTPAttributes) – Attributes to set.

fstat(file_obj)[source]

Get file stat attributes from a ssh2.sftp.SFTPHandle object.

Parameters

file_obj (ssh2.sftp.SFTPHandle) – ssh2.sftp.SFTPHandle of the file on the remote server.

fstatvfs(file_obj)[source]

Get file system statistics for a ssh2.sftp.SFTPHandle object.

Parameters

file_obj (ssh2.sftp.SFTPHandle) – ssh2.sftp.SFTPHandle of the file on the remote server.

fsync(file_obj)[source]

Tells the remote file system to synchronize the file to disk. This will only work if the SFTP session has enable_fsync set to True

Parameters

file_obj (ssh2.sftp.SFTPHandle) – ssh2.sftp.SFTPHandle of the file on the remote server.

list_dir(remote_path, remove_empty=False)[source]

Lists a directory over SFTP on the remote server.

Parameters
  • remote_path (str) – Path that file is located at on the remote server.

  • remove_empty (bool) – Remove EAGAIN records from the generator

Returns

generator of tuple

lstat(remote_path)[source]

Gets file/directory file permissions for remote_path but follows symbolic links.

Parameters

remote_path (str) – File/directory file permissions to get on the remote server.

Returns

ssh2.sftp_handle.SFTPAttributes or redssh.libssh2.LIBSSH2_ERROR_EAGAIN

mkdir(remote_path, dir_mode)[source]

Makes a directory using SFTP on the remote server.

Parameters
  • remote_path (str) – Path the directory is going to be made at on the remote server.

  • dir_mode (int) – File mode for the directory being created.

Returns

None

open(remote_path, sftp_flags, file_mode, file_obj=False)[source]

Open a file object over SFTP on the remote server.

Parameters
  • remote_path (str) – Path that file is located at on the remote server.

  • sftp_flags (int) – Flags for the SFTP session to understand what you are going to do with the file.

  • file_mode (int) – File mode for the file being opened.

  • file_obj (bool) – Return a file object instead of a ssh2.sftp.SFTPHandle

Returns

ssh2.sftp.SFTPHandle or redssh.sftp.RedSFTPFile

open_dir(remote_path)[source]

Opens a directory object over SFTP on the remote server.

Parameters

remote_path (str) – Path that file is located at on the remote server.

Returns

ssh2.sftp_handle.SFTPHandle

Raises

ssh2.exceptions.SFTPHandleError on errors opening directory.

put_file(local_path, remote_path)[source]

Upload file via SFTP to the remote session. Similar to cp /files/file /target. Also retains file permissions.

Parameters
  • local_path (str) – The local path, on the machine where your code is running from, to upload from.

  • remote_path (str) – The remote path to upload the local_path to.

put_folder(local_path, remote_path)[source]

Upload an entire folder via SFTP to the remote session. Similar to cp -r /files/* /target Also retains file permissions. Local path must be a directory to upload, if a path to a file is provided, nothing will happen.

Parameters
  • local_path (str) – The local path, on the machine where your code is running from, to upload from.

  • remote_path (str) – The remote path to upload the local_path to.

read(file_obj, iter=True)[source]

Read from file object over SFTP on the remote server.

Parameters
  • file_obj (ssh2.sftp.SFTPHandle) – ssh2.sftp.SFTPHandle to interact with.

  • iter (bool) – Flag for if you want the iterable object instead of just a byte string returned.

Returns

byte str or iter

rename(original_path, destination_path)[source]

Rename file at original_path to destination_path.

Parameters
  • original_path (str) – Original path on the remote server.

  • destination_path (str) – Destination path on the remote server.

rewind(file_obj)[source]

Rewind a file object over SFTP to the beginning.

Parameters

file_obj (ssh2.sftp.SFTPHandle) – ssh2.sftp.SFTPHandle to interact with.

Returns

None

rmdir(remote_path)[source]

Remove directory at remote_path.

Parameters

remote_path (str) – Path to the directory to remove on the remote server.

Returns

int

seek(file_obj, offset)[source]

Seek to a certain location in a file object over SFTP.

Parameters
  • file_obj (ssh2.sftp.SFTPHandle) – ssh2.sftp.SFTPHandle to interact with.

  • offset (int) – What location to seek to in the file.

Returns

None

setstat(remote_path, attrs)[source]

Sets file/directory file permissions for remote_path.

Parameters
  • remote_path (str) – Path to make changes to on the remote server.

  • attrs (int) – File mode for the remote_path given.

Returns

int

stat(remote_path)[source]

Gets file/directory file permissions for remote_path.

Parameters

remote_path (str) – File/directory file permissions to get on the remote server.

Returns

ssh2.sftp_handle.SFTPAttributes or redssh.libssh2.LIBSSH2_ERROR_EAGAIN

statvfs(remote_path)[source]

Gets file system information for remote_path.

Parameters

remote_path (str) – Path that the file system information is going to be queried for on the remote server.

Returns

ssh2.sftp_handle.SFTPStatVFS or an int of the error code from ssh2

Creates a symbolic link at path to then link to target.

Parameters
  • path (str) – Path that the symbolic link will live at on the remote server.

  • target (str) – Path that the symbolic link at path will point to on the remote server.

Returns

int

Delete or unlink file at remote_path.

Parameters

remote_path (str) – Path that file will be deleted or unlinked on the remote server.

write(file_obj, data_bytes)[source]

Write to a file object over SFTP on the remote server.

Parameters
  • file_obj (ssh2.sftp.SFTPHandle) – ssh2.sftp.SFTPHandle to interact with.

  • data_bytes (byte str) – Bytes to write to the file with.

Returns

None

class redssh.sftp.RedSFTPFile(sftp, remote_path, sftp_flags, file_mode)[source]

Bases: object

Interact with files over SFTP using a class rather than passing a file handle around.

This class simply uses the functions from redssh.sftp.RedSFTP minus any requirement for the file_obj argument for calls.

Parameters
  • sftp (redssh.sftp.RedSFTP) – redssh.sftp.RedSFTP object from the session you’d like to interact via.

  • remote_path (str) – Path that file is located at on the remote server.

  • sftp_flags (int) – Flags for the SFTP session to understand what you are going to do with the file.

  • file_mode (int) – File mode for the file being opened.