Package paramiko :: Module sftp_file :: Class SFTPFile
[frames] | no frames]

Class SFTPFile

source code

       object --+    
                |    
file.BufferedFile --+
                    |
                   SFTPFile

Proxy object for a file on the remote server, in client mode SFTP.

Instances of this class may be used as context managers in the same way that built-in Python file objects are.

Instance Methods
 
__init__(self, sftp, handle, mode='r', bufsize=-1)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
__del__(self) source code
 
close(self)
Close the file.
source code
 
settimeout(self, timeout)
Set a timeout on read/write operations on the underlying socket or ssh `.Channel`.
source code
 
gettimeout(self)
Returns the timeout in seconds (as a `float`) associated with the socket or ssh `.Channel` used for this file.
source code
 
setblocking(self, blocking)
Set blocking or non-blocking mode on the underiying socket or ssh `.Channel`.
source code
 
seek(self, offset, whence=0)
Set the file's current position, like stdio's ``fseek``.
source code
 
stat(self)
Retrieve information about this file from the remote system.
source code
 
chmod(self, mode)
Change the mode (permissions) of this file.
source code
 
chown(self, uid, gid)
Change the owner (``uid``) and group (``gid``) of this file.
source code
 
utime(self, times)
Set the access and modified times of this file.
source code
 
truncate(self, size)
Change the size of this file.
source code
 
check(self, hash_algorithm, offset=0, length=0, block_size=0)
Ask the server for a hash of a section of this file.
source code
 
set_pipelined(self, pipelined=True)
Turn on/off the pipelining of write operations to this file.
source code
 
prefetch(self)
Pre-fetch the remaining contents of this file in anticipation of future `.read` calls.
source code
 
readv(self, chunks)
Read a set of blocks from the file by (offset, length).
source code
 
__enter__(self) source code
 
__exit__(self, type, value, traceback) source code

Inherited from file.BufferedFile: __iter__, __next__, flush, next, read, readline, readlines, tell, write, writelines, xreadlines

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

Class Variables
  MAX_REQUEST_SIZE = 32768

Inherited from file.BufferedFile: FLAG_APPEND, FLAG_BINARY, FLAG_BUFFERED, FLAG_LINE_BUFFERED, FLAG_READ, FLAG_UNIVERSAL_NEWLINE, FLAG_WRITE, SEEK_CUR, SEEK_END, SEEK_SET

Properties

Inherited from file.BufferedFile: closed

Inherited from object: __class__

Method Details

__init__(self, sftp, handle, mode='r', bufsize=-1)
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__
(inherited documentation)

__del__(self)
(Destructor)

source code 
Overrides: file.BufferedFile.__del__

close(self)

source code 

Close the file.

Overrides: file.BufferedFile.close

settimeout(self, timeout)

source code 

Set a timeout on read/write operations on the underlying socket or
ssh `.Channel`.

:param float timeout:
    seconds to wait for a pending read/write operation before raising
    ``socket.timeout``, or ``None`` for no timeout

.. seealso:: `.Channel.settimeout`

gettimeout(self)

source code 

Returns the timeout in seconds (as a `float`) associated with the socket or ssh `.Channel` used for this file.

.. seealso:: `.Channel.gettimeout`

setblocking(self, blocking)

source code 

Set blocking or non-blocking mode on the underiying socket or ssh
`.Channel`.

:param int blocking:
    0 to set non-blocking mode; non-0 to set blocking mode.

.. seealso:: `.Channel.setblocking`

seek(self, offset, whence=0)

source code 

Set the file's current position, like stdio's ``fseek``.  Not all file
objects support seeking.

.. note::
    If a file is opened in append mode (``'a'`` or ``'a+'``), any seek
    operations will be undone at the next write (as the file position
    will move back to the end of the file).

:param int offset:
    position to move to within the file, relative to ``whence``.
:param int whence:
    type of movement: 0 = absolute; 1 = relative to the current
    position; 2 = relative to the end of the file.

:raises IOError: if the file doesn't support random access.

Overrides: file.BufferedFile.seek
(inherited documentation)

stat(self)

source code 

Retrieve information about this file from the remote system. This is exactly like `.SFTPClient.stat`, except that it operates on an already-open file.

:return: an `.SFTPAttributes` object containing attributes about this file.

chmod(self, mode)

source code 

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

:param int mode: new permissions

chown(self, uid, gid)

source code 

Change the owner (``uid``) and group (``gid``) of this 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 int uid: new owner's uid :param int gid: new group id

utime(self, times)

source code 

Set the access and modified times of this file.  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 tuple times:
    ``None`` or a tuple of (access time, modified time) in standard
    internet epoch time (seconds since 01 January 1970 GMT)

truncate(self, size)

source code 

Change the size of this file. This usually extends or shrinks the size of the file, just like the ``truncate()`` method on Python file objects.

:param size: the new size of the file :type size: int or long

check(self, hash_algorithm, offset=0, length=0, block_size=0)

source code 

Ask the server for a hash of a section of this file.  This can be used
to verify a successful upload or download, or for various rsync-like
operations.

The file is hashed from ``offset``, for ``length`` bytes.  If ``length``
is 0, the remainder of the file is hashed.  Thus, if both ``offset``
and ``length`` are zero, the entire file is hashed.

Normally, ``block_size`` will be 0 (the default), and this method will
return a byte string representing the requested hash (for example, a
string of length 16 for MD5, or 20 for SHA-1).  If a non-zero
``block_size`` is given, each chunk of the file (from ``offset`` to
``offset + length``) of ``block_size`` bytes is computed as a separate
hash.  The hash results are all concatenated and returned as a single
string.

For example, ``check('sha1', 0, 1024, 512)`` will return a string of
length 40.  The first 20 bytes will be the SHA-1 of the first 512 bytes
of the file, and the last 20 bytes will be the SHA-1 of the next 512
bytes.

:param str hash_algorithm:
    the name of the hash algorithm to use (normally ``"sha1"`` or
    ``"md5"``)
:param offset:
    offset into the file to begin hashing (0 means to start from the
    beginning)
:type offset: int or long
:param length:
    number of bytes to hash (0 means continue to the end of the file)
:type length: int or long
:param int block_size:
    number of bytes to hash per result (must not be less than 256; 0
    means to compute only one hash of the entire segment)
:type block_size: int
:return:
    `str` of bytes representing the hash of each block, concatenated
    together

:raises IOError: if the server doesn't support the "check-file"
    extension, or possibly doesn't support the hash algorithm
    requested
    
.. note:: Many (most?) servers don't support this extension yet.

.. versionadded:: 1.4

set_pipelined(self, pipelined=True)

source code 

Turn on/off the pipelining of write operations to this file.  When
pipelining is on, paramiko won't wait for the server response after
each write operation.  Instead, they're collected as they come in. At
the first non-write operation (including `.close`), all remaining
server responses are collected.  This means that if there was an error
with one of your later writes, an exception might be thrown from within
`.close` instead of `.write`.

By default, files are not pipelined.

:param bool pipelined:
    ``True`` if pipelining should be turned on for this file; ``False``
    otherwise

.. versionadded:: 1.5

prefetch(self)

source code 

Pre-fetch the remaining contents of this file in anticipation of future `.read` calls. If reading the entire file, pre-fetching can dramatically improve the download speed by avoiding roundtrip latency. The file's contents are incrementally buffered in a background thread.

The prefetched data is stored in a buffer until read via the `.read` method. Once data has been read, it's removed from the buffer. The data may be read in a random order (using `.seek`); chunks of the buffer that haven't been read will continue to be buffered.

.. versionadded:: 1.5.1

readv(self, chunks)

source code 

Read a set of blocks from the file by (offset, length).  This is more
efficient than doing a series of `.seek` and `.read` calls, since the
prefetch machinery is used to retrieve all the requested blocks at
once.

:param chunks:
    a list of (offset, length) tuples indicating which sections of the
    file to read
:type chunks: list(tuple(long, int))
:return: a list of blocks read, in the same order as in ``chunks``

.. versionadded:: 1.5.4