sshmitm.clients.sftp module
SFTP Client Module
This module contains the implementation of the SFTP client.
- class sshmitm.clients.sftp.SFTPClient(host, port, method, password, user, key, 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 serverusername – username for authentication to the SFTP server
password (
Optional
[str
]) – password for authentication to the SFTP serverport (
int
) – port number to use for the connection (default is 22)method (
AuthenticationMethod
) –user (
str
) –key (
Optional
[PKey
]) –session (
Session
) –
- chmod(path, mode)
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).
- Return type:
int
- Returns:
paramiko.sftp.SFTP_OK if the operation was successful, paramiko.sftp.SFTP_FAILURE otherwise.
- chown(path, uid, gid)
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.
- Return type:
int
- Returns:
paramiko.sftp.SFTP_OK if the operation was successful, paramiko.sftp.SFTP_FAILURE otherwise.
- close()
Close the SFTP session.
- Return type:
int
- Returns:
returns paramiko.sftp.SFTP_OK on success, paramiko.sftp.SFTP_FAILURE on failure
- connect()
Connect to the SFTP server.
- Return type:
bool
- Returns:
Whether the connection was successful.
- classmethod from_client(ssh_client)
Create an SFTPClient instance from an SSHClient instance.
- Parameters:
ssh_client (
Optional
[SSHClient
]) – The SSHClient instance.- Return type:
Optional
[SFTPClient
]- Returns:
An SFTPClient instance, or None if the SFTPClient could not be created.
- get(remotepath, localpath, callback=None)
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
]], default:None
) – 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.
- Return type:
int
- Returns:
paramiko.sftp.SFTP_OK if the operation was successful, paramiko.sftp.SFTP_FAILURE otherwise.
- listdir_attr(path='.')
This method returns the list of files and directories in the given path with their attributes.
- Parameters:
path (
str
, default:'.'
) – path to the directory to list the contents of. Default is current directory ‘.’- Return type:
Union
[int
,List
[SFTPAttributes
]]- Returns:
If successful, it returns a list of SFTPAttributes objects, else returns paramiko.sftp.SFTP_FAILURE
- lstat(path)
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.- Return type:
Union
[int
,SFTPAttributes
]- Returns:
If successful, it returns a SFTPAttributes object, else returns paramiko.sftp.SFTP_FAILURE
- mkdir(path, mode=511)
This method creates a new directory at the given path.
- Parameters:
path (
Union
[str
,bytes
]) – path to the directory to be created.mode (
int
, default:511
) – mode of the directory to be created. Default is 511.
- Return type:
int
- Returns:
If successful, returns paramiko.sftp.SFTP_OK, else returns paramiko.sftp.SFTP_FAILURE
- open(filename, mode='r', bufsize=-1)
Open a file on the SFTP server.
- Parameters:
filename (
Union
[str
,bytes
]) – The file to open.mode (
str
, default:'r'
) – The mode in which to open the file.bufsize (
int
, default:-1
) – The buffer size for the file.
- Return type:
SFTPFile
- Returns:
An SFTPFile instance for the opened file.
- Raises:
paramiko.SFTPError – If the handle for the SFTP client is not available.
- put(localpath, remotepath, callback=None, confirm=True)
This method is not implemented.
- Raises:
NotImplementedError
- Parameters:
localpath (
Union
[str
,bytes
]) –remotepath (
Union
[str
,bytes
]) –callback (
Optional
[Any
], default:None
) –confirm (
bool
, default:True
) –
- Return type:
None
- readlink(path)
This method returns the target of the symbolic link or a failure code.
- Parameters:
path (
Union
[str
,bytes
]) – The path of the symbolic link.- Return type:
Union
[int
,str
]- Returns:
The target of the symbolic link or a failure code.
- remove(path)
This method removes the specified file.
- Parameters:
path (
Union
[str
,bytes
]) – The path of the file to be removed.- Return type:
int
- Returns:
A success code or a failure code.
- rename(oldpath, newpath)
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.
- Return type:
int
- Returns:
A success code or a failure code.
- rmdir(path)
This method removes the specified directory.
- Parameters:
path (
Union
[str
,bytes
]) – The path of the directory to be removed.- Return type:
int
- Returns:
A success code or a failure code.
- property running: bool
Indicate whether the SFTP client is running.
- Returns:
Whether the SFTP client is running.
- stat(path)
This method returns the status of a file.
- Parameters:
path (
Union
[str
,bytes
]) – The path of the file.- Return type:
Union
[int
,SFTPAttributes
]- Returns:
The status of the file or a failure code.
- symlink(source, dest)
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.
- Return type:
int
- Returns:
returns paramiko.sftp.SFTP_OK on success, paramiko.sftp.SFTP_FAILURE on failure
- utime(path, times)
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.
- Return type:
int
- Returns:
returns paramiko.sftp.SFTP_OK on success, paramiko.sftp.SFTP_FAILURE on failure