Package paramiko :: Class SFTPClient
[show private | hide private]
[frames | no frames]

Type SFTPClient

object --+    
         |    
  BaseSFTP --+
             |
            SFTPClient

Known Subclasses:
SFTP

SFTP client object. SFTPClient is used to open an sftp session across an open ssh Transport and do remote file operations.
Method Summary
  __init__(self, sock)
Create an SFTP client from an existing Channel.
  __del__(self)
  chdir(self, path)
Change the "current directory" of this SFTP session.
  chmod(self, path, mode)
Change the mode (permissions) of a file.
  chown(self, path, uid, gid)
Change the owner (uid) and group (gid) of a file.
  close(self)
Close the SFTP session and its underlying channel.
SFTPFile file(self, filename, mode, bufsize)
Open a file on the remote server.
SFTPClient from_transport(selfclass, t)
Create an SFTP client channel from an open Transport. (Class method)
  get(self, remotepath, localpath)
Copy a remote file (remotepath) from the SFTP server to the local host as localpath.
str getcwd(self)
Return the "current working directory" for this SFTP session, as emulated by paramiko.
list of str listdir(self, path)
Return a list containing the names of the entries in the given path.
list of SFTPAttributes listdir_attr(self, path)
Return a list containing SFTPAttributes objects corresponding to files in the given path.
SFTPAttributes lstat(self, path)
Retrieve information about a file on the remote system, without following symbolic links (shortcuts).
  mkdir(self, path, mode)
Create a folder (directory) named path with numeric mode mode.
str normalize(self, path)
Return the normalized path (on the server) of a given path.
SFTPFile open(self, filename, mode, bufsize)
Open a file on the remote server.
  put(self, localpath, remotepath)
Copy a local file (localpath) to the SFTP server as remotepath.
str readlink(self, path)
Return the target of a symbolic link (shortcut).
  remove(self, path)
Remove the file at the given path.
  rename(self, oldpath, newpath)
Rename a file or folder from oldpath to newpath.
  rmdir(self, path)
Remove the folder named path.
SFTPAttributes stat(self, path)
Retrieve information about a file on the remote system.
  symlink(self, source, dest)
Create a symbolic link (shortcut) of the source path at destination.
  unlink(self, path)
Remove the file at the given path.
  utime(self, path, times)
Set the access and modified times of the file specified by path.
  _adjust_cwd(self, path)
Return an adjusted path if we're emulating a "current working directory" for the server.
  _async_request(self, fileobj, t, *arg)
  _convert_status(self, msg)
Raises EOFError or IOError on error status; otherwise does nothing.
  _finish_responses(self, fileobj)
  _read_response(self, waitfor)
  _request(self, t, *arg)
    Inherited from BaseSFTP
  _log(self, level, msg)
  _read_all(self, n)
  _read_packet(self)
  _send_packet(self, t, packet)
  _send_server_version(self)
  _send_version(self)
  _write_all(self, out)
    Inherited from object
  __delattr__(...)
x.__delattr__('name') <==> del x.name
  __getattribute__(...)
x.__getattribute__('name') <==> x.name
  __hash__(x)
x.__hash__() <==> hash(x)
  __new__(T, S, ...)
T.__new__(S, ...) -> a new object with type S, a subtype of T
  __reduce__(...)
helper for pickle
  __reduce_ex__(...)
helper for pickle
  __repr__(x)
x.__repr__() <==> repr(x)
  __setattr__(...)
x.__setattr__('name', value) <==> x.name = value
  __str__(x)
x.__str__() <==> str(x)

Instance Method Details

__init__(self, sock)
(Constructor)

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.
Parameters:
sock - an open Channel using the "sftp" subsystem.
           (type=Channel)
Overrides:
paramiko.BaseSFTP.__init__

chdir(self, path)

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.
Parameters:
path - new current working directory
           (type=str)
Raises:
IOError - if the requested path doesn't exist on the server

Since: 1.4

chmod(self, path, mode)

Change the mode (permissions) of a file. The permissions are unix-style and identical to those used by python's os.chmod function.
Parameters:
path - path of the file to change the permissions of.
           (type=string)
mode - new permissions.
           (type=int)

chown(self, path, uid, gid)

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.
Parameters:
path - path of the file to change the owner and group of.
           (type=string)
uid - new owner's uid
           (type=int)
gid - new group id
           (type=int)

close(self)

Close the SFTP session and its underlying channel.

Since: 1.4

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

Open a file on the remote server. The arguments are the same as for python's built-in file (aka open). A file-like object is returned, which closely mimics the behavior of a normal python file object.

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.
Parameters:
filename - name of the file to open.
           (type=string)
mode - mode (python-style) to open in.
           (type=string)
bufsize - desired buffering (-1 = default buffer size)
           (type=int)
Returns:
a file object representing the open file.
           (type=SFTPFile)
Raises:
IOError - if the file could not be opened.

get(self, remotepath, localpath)

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.
Parameters:
remotepath - the remote file to copy
           (type=str)
localpath - the destination path on the local host
           (type=str)

Since: 1.4

getcwd(self)

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.
Returns:
the current working directory on the server, or None
           (type=str)

Since: 1.4

listdir(self, path='.')

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.
Parameters:
path - path to list (defaults to '.')
           (type=str)
Returns:
list of filenames
           (type=list of str)

listdir_attr(self, path='.')

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.
Parameters:
path - path to list (defaults to '.')
           (type=str)
Returns:
list of attributes
           (type=list of SFTPAttributes)

Since: 1.2

lstat(self, path)

Retrieve information about a file on the remote system, without following symbolic links (shortcuts). This otherwise behaves exactly the same as stat.
Parameters:
path - the filename to stat.
           (type=string)
Returns:
an object containing attributes about the given file.
           (type=SFTPAttributes)

mkdir(self, path, mode=511)

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.
Parameters:
path - name of the folder to create.
           (type=string)
mode - permissions (posix-style) for the newly-created folder.
           (type=int)

normalize(self, path)

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).
Parameters:
path - path to be normalized.
           (type=str)
Returns:
normalized form of the given path.
           (type=str)
Raises:
IOError - if the path can't be resolved on the server

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

Open a file on the remote server. The arguments are the same as for python's built-in file (aka open). A file-like object is returned, which closely mimics the behavior of a normal python file object.

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.
Parameters:
filename - name of the file to open.
           (type=string)
mode - mode (python-style) to open in.
           (type=string)
bufsize - desired buffering (-1 = default buffer size)
           (type=int)
Returns:
a file object representing the open file.
           (type=SFTPFile)
Raises:
IOError - if the file could not be opened.

put(self, localpath, remotepath)

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.
Parameters:
localpath - the local file to copy
           (type=str)
remotepath - the destination path on the SFTP server
           (type=str)

Since: 1.4

readlink(self, path)

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.
Parameters:
path - path of the symbolic link file.
           (type=str)
Returns:
target path.
           (type=str)

remove(self, path)

Remove the file at the given path.
Parameters:
path - path (absolute or relative) of the file to remove.
           (type=string)
Raises:
IOError - if the path refers to a folder (directory). Use rmdir to remove a folder.

rename(self, oldpath, newpath)

Rename a file or folder from oldpath to newpath.
Parameters:
oldpath - existing name of the file or folder.
           (type=string)
newpath - new name for the file or folder.
           (type=string)
Raises:
IOError - if newpath is a folder, or something else goes wrong.

rmdir(self, path)

Remove the folder named path.
Parameters:
path - name of the folder to remove.
           (type=string)

stat(self, path)

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 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.
Parameters:
path - the filename to stat.
           (type=string)
Returns:
an object containing attributes about the given file.
           (type=SFTPAttributes)

symlink(self, source, dest)

Create a symbolic link (shortcut) of the source path at destination.
Parameters:
source - path of the original file.
           (type=string)
dest - path of the newly created symlink.
           (type=string)

unlink(self, path)

Remove the file at the given path.
Parameters:
path - path (absolute or relative) of the file to remove.
           (type=string)
Raises:
IOError - if the path refers to a folder (directory). Use rmdir to remove a folder.

utime(self, path, times)

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.
Parameters:
path - path of the file to modify.
           (type=string)
times - None or a tuple of (access time, modified time) in standard internet epoch time (seconds since 01 January 1970 GMT).
           (type=tuple of int)

_adjust_cwd(self, path)

Return an adjusted path if we're emulating a "current working directory" for the server.

_convert_status(self, msg)

Raises EOFError or IOError on error status; otherwise does nothing.

Class Method Details

from_transport(selfclass, t)

Create an SFTP client channel from an open Transport.
Parameters:
t - an open Transport which is already authenticated.
           (type=Transport)
Returns:
a new SFTPClient object, referring to an sftp session (channel) across the transport.
           (type=SFTPClient)

Generated by Epydoc 2.1 on Sun Dec 4 11:16:47 2005 http://epydoc.sf.net