sshmitm.clients.sftp module

SFTP Client Module

This module contains the implementation of the SFTP client.

class sshmitm.clients.sftp.SFTPClient(host: str, port: int, method: AuthenticationMethod, password: Optional[str], user: str, key: Optional[PKey], session: Session)

Bases: SSHClient

This class implements a simple SFTP client for transferring files to and from a remote server using SFTP protocol.

Parameters:
  • host (str) – hostname or IP address of the SFTP server

  • username (str) – username for authentication to the SFTP server

  • password (str) – password for authentication to the SFTP server

  • port (int) – port number to use for the connection (default is 22)

chmod(path: Union[str, bytes], mode: int) int

Changes the mode (permission) of the specified path.

Parameters:
  • path (Union[str, bytes]) – The path of the file or directory whose permissions are to be changed.

  • mode (int) – The new permission mode, expressed as an integer (e.g. 0o755).

Returns:

paramiko.sftp.SFTP_OK if the operation was successful, paramiko.sftp.SFTP_FAILURE otherwise.

Return type:

int

chown(path: Union[str, bytes], uid: int, gid: int) int

Changes the owner and group of the specified path.

Parameters:
  • path (Union[str, bytes]) – The path of the file or directory whose ownership is to be changed.

  • uid (int) – The new user ID of the file or directory.

  • gid (int) – The new group ID of the file or directory.

Returns:

paramiko.sftp.SFTP_OK if the operation was successful, paramiko.sftp.SFTP_FAILURE otherwise.

Return type:

int

close() int

Close the SFTP session.

Returns:

returns paramiko.sftp.SFTP_OK on success, paramiko.sftp.SFTP_FAILURE on failure

Return type:

int

connect() bool

Connect to the SFTP server.

Returns:

Whether the connection was successful.

Return type:

bool

classmethod from_client(ssh_client: Optional[SSHClient]) Optional[SFTPClient]

Create an SFTPClient instance from an SSHClient instance.

Parameters:

ssh_client (Optional[SSHClient]) – The SSHClient instance.

Returns:

An SFTPClient instance, or None if the SFTPClient could not be created.

Return type:

Optional[SFTPClient]

get(remotepath: Union[str, bytes], localpath: Union[str, bytes], callback: Optional[Callable[[int, int], Any]] = None) int

Downloads a file from the remote SFTP server and saves it to the local file system.

Parameters:
  • remotepath (Union[str, bytes]) – The path of the file on the remote SFTP server.

  • localpath (Union[str, bytes]) – The path of the file on the local file system.

  • callback (Optional[Callable[[int, int], Any]]) – An optional callback function that is called after each chunk of data has been transmitted. The function should accept two arguments: the number of bytes transmitted so far, and the total size of the file in bytes.

Returns:

paramiko.sftp.SFTP_OK if the operation was successful, paramiko.sftp.SFTP_FAILURE otherwise.

Return type:

int

listdir_attr(path: str = '.') Union[int, List[SFTPAttributes]]

This method returns the list of files and directories in the given path with their attributes.

Parameters:

path (str) – path to the directory to list the contents of. Default is current directory ‘.’

Returns:

If successful, it returns a list of SFTPAttributes objects, else returns paramiko.sftp.SFTP_FAILURE

Return type:

Union[int, List[SFTPAttributes]]

lstat(path: Union[str, bytes]) Union[int, SFTPAttributes]

This method returns the attributes of the file/directory at the given path.

Parameters:

path (Union[str, bytes]) – path to the file/directory to get the attributes of.

Returns:

If successful, it returns a SFTPAttributes object, else returns paramiko.sftp.SFTP_FAILURE

Return type:

Union[int, SFTPAttributes]

mkdir(path: Union[str, bytes], mode: int = 511) int

This method creates a new directory at the given path.

Parameters:
  • path (Union[str, bytes]) – path to the directory to be created.

  • mode (int) – mode of the directory to be created. Default is 511.

Returns:

If successful, returns paramiko.sftp.SFTP_OK, else returns paramiko.sftp.SFTP_FAILURE

Return type:

int

open(filename: Union[str, bytes], mode: str = 'r', bufsize: int = -1) SFTPFile

Open a file on the SFTP server.

Parameters:
  • filename (Union[str, bytes]) – The file to open.

  • mode (str) – The mode in which to open the file.

  • bufsize (int) – The buffer size for the file.

Returns:

An SFTPFile instance for the opened file.

Return type:

SFTPFile

Raises:

paramiko.SFTPError – If the handle for the SFTP client is not available.

put(localpath: Union[str, bytes], remotepath: Union[str, bytes], callback: Optional[Any] = None, confirm: bool = True) None

This method is not implemented.

Raises:

NotImplementedError

This method returns the target of the symbolic link or a failure code.

Parameters:

path (Union[str, bytes]) – The path of the symbolic link.

Returns:

The target of the symbolic link or a failure code.

Return type:

Union[int, str]

remove(path: Union[str, bytes]) int

This method removes the specified file.

Parameters:

path (Union[str, bytes]) – The path of the file to be removed.

Returns:

A success code or a failure code.

Return type:

int

rename(oldpath: Union[str, bytes], newpath: Union[str, bytes]) int

This method renames a file.

Parameters:
  • oldpath (Union[str, bytes]) – The current name of the file.

  • newpath (Union[str, bytes]) – The new name of the file.

Returns:

A success code or a failure code.

Return type:

int

rmdir(path: Union[str, bytes]) int

This method removes the specified directory.

Parameters:

path (Union[str, bytes]) – The path of the directory to be removed.

Returns:

A success code or a failure code.

Return type:

int

property running: bool

Indicate whether the SFTP client is running.

Returns:

Whether the SFTP client is running.

Return type:

bool

stat(path: Union[str, bytes]) Union[int, SFTPAttributes]

This method returns the status of a file.

Parameters:

path (Union[str, bytes]) – The path of the file.

Returns:

The status of the file or a failure code.

Return type:

Union[int, SFTPAttributes]

Create a symbolic link pointing to source at dest.

Parameters:
  • source (Union[str, bytes]) – The target file of the symbolic link.

  • dest (Union[str, bytes]) – The path to the symbolic link.

Returns:

returns paramiko.sftp.SFTP_OK on success, paramiko.sftp.SFTP_FAILURE on failure

Return type:

int

utime(path: Union[str, bytes], times: Tuple[float, float]) int

Update the access and modification time of a file.

Parameters:
  • path (Union[str, bytes]) – The path to the file.

  • times (Tuple[float, float]) – Tuple of float representing the access and modification time.

Returns:

returns paramiko.sftp.SFTP_OK on success, paramiko.sftp.SFTP_FAILURE on failure

Return type:

int