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

# Class SFTPHandle

source code

    object --+
             |
            SFTPHandle

Abstract object representing a handle to an open file (or folder) in an SFTP server implementation. Each handle has a string representation used by the client to refer to the underlying file.

Server implementations can (and should) subclass SFTPHandle to implement features of a file handle, like stat or chattr.

Instance Methods
__init__(self, flags=0) Create a new file handle representing a local file being served over SFTP. | source code
int |
chattr(self, attr) Change the attributes of this file. | source code
close(self) When a client closes a file, this method is called on the handle. | source code
str |
read(self, offset, length) Read up to `length` bytes from this file, starting at position `offset`. | source code
SFTPAttributes or error code |
stat(self) Return an SFTPAttributes object referring to this open file, or an error code. | source code
write(self, offset, data) Write `data` into this file at position `offset`. | source code
Inherited from `object`: `__delattr__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__repr__`, `__setattr__`, `__str__`

Properties
Inherited from `object`: `__class__`

Method Details

### __init__(self, flags=0) (Constructor) | source code
Create a new file handle representing a local file being served over SFTP. If `flags` is passed in, it's used to determine if the file is open in append mode. Parameters: `flags` (int) - optional flags as passed to SFTPServerInterface.open Overrides: object.__init__

### chattr(self, attr) | source code
Change the attributes of this file. The `attr` object will contain only those fields provided by the client in its request, so you should check for the presence of fields before using them. Parameters: `attr` (SFTPAttributes) - the attributes to change on this file. Returns: int an error code like SFTP_OK.

### close(self) | source code
When a client closes a file, this method is called on the handle. Normally you would use this method to close the underlying OS level file object(s). The default implementation checks for attributes on `self` named `readfile` and/or `writefile`, and if either or both are present, their `close()` methods are called. This means that if you are using the default implementations of read and write, this method's default implementation should be fine also.

### read(self, offset, length) | source code
Read up to `length` bytes from this file, starting at position `offset`. The offset may be a python long, since SFTP allows it to be 64 bits. If the end of the file has been reached, this method may return an empty string to signify EOF, or it may also return SFTP_EOF. The default implementation checks for an attribute on `self` named `readfile`, and if present, performs the read operation on the python file-like object found there. (This is meant as a time saver for the common case where you are wrapping a python file object.) Parameters: `offset` (int or long) - position in the file to start reading from. `length` (int) - number of bytes to attempt to read. Returns: str data read from the file, or an SFTP error code.

### stat(self) | source code
Return an SFTPAttributes object referring to this open file, or an error code. This is equivalent to SFTPServerInterface.stat, except it's called on an open file instead of a path. Returns: SFTPAttributes or error code an attributes object for the given file, or an SFTP error code (like SFTP_PERMISSION_DENIED).

### write(self, offset, data) | source code
Write `data` into this file at position `offset`. Extending the file past its original end is expected. Unlike python's normal `write()` methods, this method cannot do a partial write: it must write all of `data` or else return an error. The default implementation checks for an attribute on `self` named `writefile`, and if present, performs the write operation on the python file-like object found there. The attribute is named differently from `readfile` to make it easy to implement read-only (or write-only) files, but if both attributes are present, they should refer to the same file. Parameters: `offset` (int or long) - position in the file to start reading from. `data` (str) - data to write into the file. Returns: an SFTP error code like SFTP_OK.