Package paramiko :: Module sftp_client :: Class SFTPClient
[frames] | no frames]

Class SFTPClient

source code

   object --+    
            |    
sftp.BaseSFTP --+
                |
               SFTPClient
Known Subclasses:

SFTP client object.

Used to open an SFTP session across an open SSH `.Transport` and perform remote file operations.

Instance Methods
 
__init__(self, sock)
Create an SFTP client from an existing `.Channel`.
source code
 
close(self)
Close the SFTP session and its underlying channel.
source code
 
get_channel(self)
Return the underlying `.Channel` object for this SFTP session.
source code
 
listdir(self, path='.')
Return a list containing the names of the entries in the given ``path``.
source code
 
listdir_attr(self, path='.')
Return a list containing `.SFTPAttributes` objects corresponding to files in the given ``path``.
source code
 
open(self, filename, mode='r', bufsize=-1)
Open a file on the remote server.
source code
 
file(self, filename, mode='r', bufsize=-1)
Open a file on the remote server.
source code
 
remove(self, path)
Remove the file at the given path.
source code
 
unlink(self, path)
Remove the file at the given path.
source code
 
rename(self, oldpath, newpath)
Rename a file or folder from ``oldpath`` to ``newpath``.
source code
 
mkdir(self, path, mode=511)
Create a folder (directory) named ``path`` with numeric mode ``mode``.
source code
 
rmdir(self, path)
Remove the folder named ``path``.
source code
 
stat(self, path)
Retrieve information about a file on the remote system.
source code
 
lstat(self, path)
Retrieve information about a file on the remote system, without following symbolic links (shortcuts).
source code
 
symlink(self, source, dest)
Create a symbolic link (shortcut) of the ``source`` path at ``destination``.
source code
 
chmod(self, path, mode)
Change the mode (permissions) of a file.
source code
 
chown(self, path, uid, gid)
Change the owner (``uid``) and group (``gid``) of a file.
source code
 
utime(self, path, times)
Set the access and modified times of the file specified by ``path``.
source code
 
truncate(self, path, size)
Change the size of the file specified by ``path``.
source code
 
readlink(self, path)
Return the target of a symbolic link (shortcut).
source code
 
normalize(self, path)
Return the normalized path (on the server) of a given path.
source code
 
chdir(self, path=None)
Change the "current directory" of this SFTP session.
source code
 
getcwd(self)
Return the "current working directory" for this SFTP session, as emulated by Paramiko.
source code
 
putfo(self, fl, remotepath, file_size=0, callback=None, confirm=True)
Copy the contents of an open file object (``fl``) to the SFTP server as ``remotepath``.
source code
 
put(self, localpath, remotepath, callback=None, confirm=True)
Copy a local file (``localpath``) to the SFTP server as ``remotepath``.
source code
 
getfo(self, remotepath, fl, callback=None)
Copy a remote file (``remotepath``) from the SFTP server and write to an open file or file-like object, ``fl``.
source code
 
get(self, remotepath, localpath, callback=None)
Copy a remote file (``remotepath``) from the SFTP server to the local host as ``localpath``.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Methods
 
from_transport(cls, t)
Create an SFTP client channel from an open `.Transport`.
source code
Properties

Inherited from object: __class__

Method Details

__init__(self, sock)
(Constructor)

source code 

Create an SFTP client from an existing `.Channel`.  The channel
should already have requested the ``"sftp"`` subsystem.

An alternate way to create an SFTP client context is by using
`from_transport`.

:param .Channel sock: an open `.Channel` using the ``"sftp"`` subsystem

:raises SSHException: if there's an exception while negotiating
    sftp

Overrides: object.__init__

from_transport(cls, t)
Class Method

source code 

Create an SFTP client channel from an open `.Transport`.

:param .Transport t: an open `.Transport` which is already authenticated
:return:
    a new `.SFTPClient` object, referring to an sftp session (channel)
    across the transport

close(self)

source code 

Close the SFTP session and its underlying channel.

.. versionadded:: 1.4

get_channel(self)

source code 

Return the underlying `.Channel` object for this SFTP session. This might be useful for doing things like setting a timeout on the channel.

.. versionadded:: 1.7.1

listdir(self, path='.')

source code 

Return a list containing the names of the entries in the given ``path``.

The list is in arbitrary order. It does not include the special entries ``'.'`` and ``'..'`` even if they are present in the folder. This method is meant to mirror ``os.listdir`` as closely as possible. For a list of full `.SFTPAttributes` objects, see `listdir_attr`.

:param str path: path to list (defaults to ``'.'``)

listdir_attr(self, path='.')

source code 

Return a list containing `.SFTPAttributes` objects corresponding to files in the given ``path``. The list is in arbitrary order. It does not include the special entries ``'.'`` and ``'..'`` even if they are present in the folder.

The returned `.SFTPAttributes` objects will each have an additional field: ``longname``, which may contain a formatted string of the file's attributes, in unix format. The content of this string will probably depend on the SFTP server implementation.

:param str path: path to list (defaults to ``'.'``) :return: list of `.SFTPAttributes` objects

.. versionadded:: 1.2

open(self, filename, mode='r', bufsize=-1)

source code 

Open a file on the remote server. The arguments are the same as for Python's built-in `python:file` (aka `python:open`). A file-like object is returned, which closely mimics the behavior of a normal Python file object, including the ability to be used as a context manager.

The mode indicates how the file is to be opened: ``'r'`` for reading, ``'w'`` for writing (truncating an existing file), ``'a'`` for appending, ``'r+'`` for reading/writing, ``'w+'`` for reading/writing (truncating an existing file), ``'a+'`` for reading/appending. The Python ``'b'`` flag is ignored, since SSH treats all files as binary. The ``'U'`` flag is supported in a compatible way.

Since 1.5.2, an ``'x'`` flag indicates that the operation should only succeed if the file was created and did not previously exist. This has no direct mapping to Python's file flags, but is commonly known as the ``O_EXCL`` flag in posix.

The file will be buffered in standard Python style by default, but can be altered with the ``bufsize`` parameter. ``0`` turns off buffering, ``1`` uses line buffering, and any number greater than 1 (``>1``) uses that specific buffer size.

:param str filename: name of the file to open :param str mode: mode (Python-style) to open in :param int bufsize: desired buffering (-1 = default buffer size) :return: an `.SFTPFile` object representing the open file

:raises IOError: if the file could not be opened.

file(self, filename, mode='r', bufsize=-1)

source code 

Open a file on the remote server. The arguments are the same as for Python's built-in `python:file` (aka `python:open`). A file-like object is returned, which closely mimics the behavior of a normal Python file object, including the ability to be used as a context manager.

The mode indicates how the file is to be opened: ``'r'`` for reading, ``'w'`` for writing (truncating an existing file), ``'a'`` for appending, ``'r+'`` for reading/writing, ``'w+'`` for reading/writing (truncating an existing file), ``'a+'`` for reading/appending. The Python ``'b'`` flag is ignored, since SSH treats all files as binary. The ``'U'`` flag is supported in a compatible way.

Since 1.5.2, an ``'x'`` flag indicates that the operation should only succeed if the file was created and did not previously exist. This has no direct mapping to Python's file flags, but is commonly known as the ``O_EXCL`` flag in posix.

The file will be buffered in standard Python style by default, but can be altered with the ``bufsize`` parameter. ``0`` turns off buffering, ``1`` uses line buffering, and any number greater than 1 (``>1``) uses that specific buffer size.

:param str filename: name of the file to open :param str mode: mode (Python-style) to open in :param int bufsize: desired buffering (-1 = default buffer size) :return: an `.SFTPFile` object representing the open file

:raises IOError: if the file could not be opened.

remove(self, path)

source code 

Remove the file at the given path. This only works on files; for removing folders (directories), use `rmdir`.

:param str path: path (absolute or relative) of the file to remove

:raises IOError: if the path refers to a folder (directory)

unlink(self, path)

source code 

Remove the file at the given path. This only works on files; for removing folders (directories), use `rmdir`.

:param str path: path (absolute or relative) of the file to remove

:raises IOError: if the path refers to a folder (directory)

rename(self, oldpath, newpath)

source code 

Rename a file or folder from ``oldpath`` to ``newpath``.

:param str oldpath: existing name of the file or folder
:param str newpath: new name for the file or folder

:raises IOError: if ``newpath`` is a folder, or something else goes
    wrong

mkdir(self, path, mode=511)

source code 

Create a folder (directory) named ``path`` with numeric mode ``mode``. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out.

:param str path: name of the folder to create :param int mode: permissions (posix-style) for the newly-created folder

rmdir(self, path)

source code 

Remove the folder named ``path``.

:param str path: name of the folder to remove

stat(self, path)

source code 

Retrieve information about a file on the remote system.  The return
value is an object whose attributes correspond to the attributes of
Python's ``stat`` structure as returned by ``os.stat``, except that it
contains fewer fields.  An SFTP server may return as much or as little
info as it wants, so the results may vary from server to server.

Unlike a Python `python:stat` object, the result may not be accessed as
a tuple.  This is mostly due to the author's slack factor.

The fields supported are: ``st_mode``, ``st_size``, ``st_uid``,
``st_gid``, ``st_atime``, and ``st_mtime``.

:param str path: the filename to stat
:return:
    an `.SFTPAttributes` object containing attributes about the given
    file

lstat(self, path)

source code 

Retrieve information about a file on the remote system, without
following symbolic links (shortcuts).  This otherwise behaves exactly
the same as `stat`.

:param str path: the filename to stat
:return:
    an `.SFTPAttributes` object containing attributes about the given
    file

symlink(self, source, dest)

source code 

Create a symbolic link (shortcut) of the ``source`` path at ``destination``.

:param str source: path of the original file :param str dest: path of the newly created symlink

chmod(self, path, mode)

source code 

Change the mode (permissions) of a file. The permissions are unix-style and identical to those used by Python's `os.chmod` function.

:param str path: path of the file to change the permissions of :param int mode: new permissions

chown(self, path, uid, gid)

source code 

Change the owner (``uid``) and group (``gid``) of a file. As with Python's `os.chown` function, you must pass both arguments, so if you only want to change one, use `stat` first to retrieve the current owner and group.

:param str path: path of the file to change the owner and group of :param int uid: new owner's uid :param int gid: new group id

utime(self, path, times)

source code 

Set the access and modified times of the file specified by ``path``.  If
``times`` is ``None``, then the file's access and modified times are set
to the current time.  Otherwise, ``times`` must be a 2-tuple of numbers,
of the form ``(atime, mtime)``, which is used to set the access and
modified times, respectively.  This bizarre API is mimicked from Python
for the sake of consistency -- I apologize.

:param str path: path of the file to modify
:param tuple times:
    ``None`` or a tuple of (access time, modified time) in standard
    internet epoch time (seconds since 01 January 1970 GMT)

truncate(self, path, size)

source code 

Change the size of the file specified by ``path``. This usually extends or shrinks the size of the file, just like the `~file.truncate` method on Python file objects.

:param str path: path of the file to modify :param size: the new size of the file :type size: int or long

readlink(self, path)

source code 

Return the target of a symbolic link (shortcut). You can use `symlink` to create these. The result may be either an absolute or relative pathname.

:param str path: path of the symbolic link file :return: target path, as a `str`

normalize(self, path)

source code 

Return the normalized path (on the server) of a given path. This can be used to quickly resolve symbolic links or determine what the server is considering to be the "current folder" (by passing ``'.'`` as ``path``).

:param str path: path to be normalized :return: normalized form of the given path (as a `str`)

:raises IOError: if the path can't be resolved on the server

chdir(self, path=None)

source code 

Change the "current directory" of this SFTP session. Since SFTP doesn't really have the concept of a current working directory, this is emulated by Paramiko. Once you use this method to set a working directory, all operations on this `.SFTPClient` object will be relative to that path. You can pass in ``None`` to stop using a current working directory.

:param str path: new current working directory

:raises IOError: if the requested path doesn't exist on the server

.. versionadded:: 1.4

getcwd(self)

source code 

Return the "current working directory" for this SFTP session, as emulated by Paramiko. If no directory has been set with `chdir`, this method will return ``None``.

.. versionadded:: 1.4

putfo(self, fl, remotepath, file_size=0, callback=None, confirm=True)

source code 

Copy the contents of an open file object (``fl``) to the SFTP server as
``remotepath``. Any exception raised by operations will be passed
through.

The SFTP operations use pipelining for speed.

:param file fl: opened file or file-like object to copy
:param str remotepath: the destination path on the SFTP server
:param int file_size:
    optional size parameter passed to callback. If none is specified,
    size defaults to 0
:param callable callback:
    optional callback function (form: ``func(int, int)``) that accepts
    the bytes transferred so far and the total bytes to be transferred
    (since 1.7.4)
:param bool confirm:
    whether to do a stat() on the file afterwards to confirm the file
    size (since 1.7.7)

:return:
    an `.SFTPAttributes` object containing attributes about the given
    file.

.. versionadded:: 1.4
.. versionchanged:: 1.7.4
    Began returning rich attribute objects.

put(self, localpath, remotepath, callback=None, confirm=True)

source code 

Copy a local file (``localpath``) to the SFTP server as ``remotepath``.
Any exception raised by operations will be passed through.  This
method is primarily provided as a convenience.

The SFTP operations use pipelining for speed.

:param str localpath: the local file to copy
:param str remotepath: the destination path on the SFTP server
:param callable callback:
    optional callback function (form: ``func(int, int)``) that accepts
    the bytes transferred so far and the total bytes to be transferred
:param bool confirm:
    whether to do a stat() on the file afterwards to confirm the file
    size

:return: an `.SFTPAttributes` object containing attributes about the given file

.. versionadded:: 1.4
.. versionchanged:: 1.7.4
    ``callback`` and rich attribute return value added.   
.. versionchanged:: 1.7.7
    ``confirm`` param added.

getfo(self, remotepath, fl, callback=None)

source code 

Copy a remote file (``remotepath``) from the SFTP server and write to
an open file or file-like object, ``fl``.  Any exception raised by
operations will be passed through.  This method is primarily provided
as a convenience.

:param object remotepath: opened file or file-like object to copy to
:param str fl:
    the destination path on the local host or open file object
:param callable callback:
    optional callback function (form: ``func(int, int)``) that accepts
    the bytes transferred so far and the total bytes to be transferred
:return: the `number <int>` of bytes written to the opened file object

.. versionadded:: 1.4
.. versionchanged:: 1.7.4
    Added the ``callable`` param.

get(self, remotepath, localpath, callback=None)

source code 

Copy a remote file (``remotepath``) from the SFTP server to the local
host as ``localpath``.  Any exception raised by operations will be
passed through.  This method is primarily provided as a convenience.

:param str remotepath: the remote file to copy
:param str localpath: the destination path on the local host
:param callable callback:
    optional callback function (form: ``func(int, int)``) that accepts
    the bytes transferred so far and the total bytes to be transferred

.. versionadded:: 1.4
.. versionchanged:: 1.7.4
    Added the ``callback`` param