Search code examples
pythonpython-3.xsftpparamiko

Attributes in Paramiko SFTPAttributes


http://docs.paramiko.org/en/stable/api/sftp.html#paramiko.sftp_attr.SFTPAttributes shows the documenation for SFTP Attributes, now

I have executed following code in terminal

>>> import paramiko
>>> from stat import S_ISDIR
>>> 
>>> client = paramiko.SSHClient()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect('ipaddress', username='xxx', password='xxx', banner_timeout=60)
>>> sftp = client.open_sftp()
>>> dl= '/home/generic/'
>>> a=sftp.listdir_attr(dl)
>>> print(a)

[<SFTPAttributes: [ size=4096 uid=1000 gid=1000 mode=040775 atime=1624152009 mtime=1622179445 ]>, <SFTPAttributes: [ size=4096 uid=1000 gid=1000 mode=040775 atime=1624172765 mtime=1623920761 ]>, <SFTPAttributes: [ size=1100 uid=1000 gid=1000 mode=0100600 atime=1624121097 mtime=1624121097 ]>]

I got the SFTP attributes as explained in documentation.

But, my doubt is when I inspect each item returned above, like dir(a[0]) it has following items:

>>> dir(a[0])
['FLAG_AMTIME', 'FLAG_EXTENDED', 'FLAG_PERMISSIONS', 'FLAG_SIZE', 'FLAG_UIDGID', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_debug_str', '_flags', '_from_msg', '_pack', '_rwx', '_unpack', 'asbytes', 'attr', 'filename', 'from_stat', 'longname', 'st_atime', 'st_gid', 'st_mode', 'st_mtime', 'st_size', 'st_uid']

>>> type(a[0])
<class 'paramiko.sftp_attr.SFTPAttributes'>
>>> 

Documentation passed above doesn't give hints about filename, longname, asbytes etc. From where this attributes are added? How can I understand it from documentation itself?


Solution

  • Your question is bit vague, so I can only quote the SFTPAttributes documentation:

    Representation of the attributes of a file (or proxied file) for SFTP in client or server mode. It attemps to mirror the object returned by os.stat as closely as possible, so it may have the following fields, with the same meanings as those returned by an os.stat object:

    • st_size
    • st_uid
    • st_gid
    • st_mode
    • st_atime
    • st_mtime

    Because SFTP allows flags to have other arbitrary named attributes, these are stored in a dict named attr. Occasionally, the filename is also stored, in filename.

    The only attribute not mentioned in the docs is longname, what usually contains a string similar to an output of *nix ls command.

    Some of the rest, like asbytes and from_stat, are low-level methods, not attributes. Unless you have specific needs, you can ignore these.