-1

I have a small utility that scans ftp server and does some analytics around available data. This code works fine for many servers but does not work for one particular server. I am sure the data is available on the server, lots of data, but it only lists the top folders and when goes inside of them just gets nothing.

Nothing fails, just empty data in the listdir_attr

I tried looking into the connection options that may influence this, but there doesn't seem any relevant. Has anyone faced similar issues?

import pysftp
import os


class Connector:

    def __init__(self, host: str, user: str, password: str, port: int = 22, root: str = "."):
        ops = pysftp.CnOpts()
        ops.hostkeys = None
        self.root = root
        self.client = pysftp.Connection(host=host, username=user, password=password, port=port, cnopts=ops)
        self.stats = list()

    def __looper(self, folder):
        for f in self.client.listdir_attr(folder):
            f.attr["path"] = folder
            f.attr["props"] = f.longname[:10]
            self.stats.append(f)
            if f.longname.startswith("d"):
                self.__looper(os.path.join(folder, f.filename))

    def build_stats(self):
        self.__looper(self.root)
        self.client.close()
        return self.stats

abolotnov
  • 4,282
  • 9
  • 56
  • 88

1 Answers1

0

here's what's fixed it for me:

Some FTP servers seem to be picky around \ vs / in the path. Many seem to don't care, but this particular one started working fine when I replaced the back slashes generated by os.path.join() with forward slashes.

Hope it helps someone.

abolotnov
  • 4,282
  • 9
  • 56
  • 88
  • 1) FTP and SFTP are completely different protocols. 2) SFTP always uses forward slashes (while some [Windows?] servers might tolerate backslashes.) => You should never use `os.path.join` on FTP path. – Martin Prikryl Apr 06 '23 at 04:30