paramiko.SFTPFile

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

# Class SFTPFile

source code

      object --+
               |
    BufferedFile --+
                   |
                  SFTPFile

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

Instance Methods
__del__(self) | source code
__init__(self, sftp, handle, mode=`'``r``'`, bufsize=-1) x.__init__(...) initializes x; see x.__class__.__doc__ for signature | source code
str |
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
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
close(self) Close the file. | source code
float |
gettimeout(self) Returns the timeout in seconds (as a float) associated with the socket or ssh Channel used for this file. | source code
prefetch(self) Pre-fetch the remaining contents of this file in anticipation of future read calls. | source code
list(str) |
readv(self, chunks) Read a set of blocks from the file by (offset, length). | source code
seek(self, offset, whence=0) Set the file's current position, like stdio's `fseek`. | source code
set_pipelined(self, pipelined=True) Turn on/off the pipelining of write operations to this file. | source code
setblocking(self, blocking) Set blocking or non-blocking mode on the underiying socket or ssh Channel. | source code
settimeout(self, timeout) Set a timeout on read/write operations on the underlying socket or ssh Channel. | source code
SFTPAttributes |
stat(self) Retrieve information about this file from the remote system. | source code
truncate(self, size) Change the size of this file. | source code
utime(self, times) Set the access and modified times of this file. | source code
Inherited from `BufferedFile`: `__iter__`, `flush`, `next`, `read`, `readline`, `readlines`, `tell`, `write`, `writelines`, `xreadlines` Inherited from `object`: `__delattr__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__repr__`, `__setattr__`, `__str__`

Class Variables
| MAX_REQUEST_SIZE = `32768`
Inherited from `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 `object`: `__class__`

Method Details

### __del__(self) (Destructor) | source code
Overrides: BufferedFile.__del__

### __init__(self, sftp, handle, mode=`'``r``'`, bufsize=-1) (Constructor) | source code
x.__init__(...) initializes x; see x.__class__.__doc__ for signature Overrides: object.__init__ (inherited documentation)

### 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. Parameters: `hash_algorithm` (str) - the name of the hash algorithm to use (normally `"sha1"` or `"md5"`) `offset` (int or long) - offset into the file to begin hashing (0 means to start from the beginning) `length` (int or long) - number of bytes to hash (0 means continue to the end of the file) `block_size` (int) - number of bytes to hash per result (must not be less than 256; 0 means to compute only one hash of the entire segment) Returns: str string 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. Since: 1.4

### 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. Parameters: `mode` (int) - 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. Parameters: `uid` (int) - new owner's uid `gid` (int) - new group id

### close(self) | source code
Close the file. Future read and write operations will fail. Overrides: BufferedFile.close (inherited documentation)

### gettimeout(self) | source code
Returns the timeout in seconds (as a float) associated with the socket or ssh Channel used for this file. Returns: float See Also: Channel.gettimeout

### 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. Since: 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. Parameters: `chunks` (list(tuple(long, int))) - a list of (offset, length) tuples indicating which sections of the file to read Returns: list(str) a list of blocks read, in the same order as in `chunks` Since: 1.5.4

### seek(self, offset, whence=0) | source code
Set the file's current position, like stdio's `fseek`. Not all file objects support seeking. Parameters: `offset` - position to move to within the file, relative to `whence`. `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: BufferedFile.seek (inherited documentation)

### 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. Parameters: `pipelined` (bool) - `True` if pipelining should be turned on for this file; `False` otherwise Since: 1.5

### setblocking(self, blocking) | source code
Set blocking or non-blocking mode on the underiying socket or ssh Channel. Parameters: `blocking` (int) - 0 to set non-blocking mode; non-0 to set blocking mode. See Also: Channel.setblocking

### settimeout(self, timeout) | source code
Set a timeout on read/write operations on the underlying socket or ssh Channel. Parameters: `timeout` (float) - seconds to wait for a pending read/write operation before raising `socket.timeout`, or `None` for no timeout See Also: Channel.settimeout

### stat(self) | source code
Retrieve information about this file from the remote system. This is exactly like SFTP.stat, except that it operates on an already-open file. Returns: SFTPAttributes an object containing attributes about this file.

### 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. Parameters: `size` (int or long) - the new size of the file

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