高度なPython FTP-ftplibがサーバーと通信する方法を制御できますか?

StackOverflow https://stackoverflow.com/questions/210067

  •  03-07-2019
  •  | 
  •  

質問

非常に具体的な(非標準の)文字列をFTPサーバーに送信する必要があります:

dir "SYS:\IC.ICAMA."

引用のスタイルとその内容がそうであるように、大文字と小文字は重要です。

残念ながら、ftplib.dir()は 'dir'ではなく 'LIST'コマンドを使用しているようです(そして、このアプリケーションでは間違ったケースを使用しています)。

FTPサーバーは実際には電話交換機であり、非常に非標準的な実装です。

ftplib.sendcmd()を使用しようとしましたが、コマンドシーケンスの一部として「pasv」も送信します。

特定のコマンドをFTPサーバーに発行する簡単な方法はありますか?

役に立ちましたか?

解決

次を試してください。これは、" dir"を使用する元の FTP.dir コマンドの修正です。 「LIST」の代わりに。 「DIRが理解できません」と表示されます。テストしたftpサーバーでエラーが発生しましたが、目的のコマンドが送信されます。 (これを確認するために使用した印刷コマンドを削除する必要があります。)

import ftplib

class FTP(ftplib.FTP):

    def shim_dir(self, *args):
        '''List a directory in long form.
        By default list current directory to stdout.
        Optional last argument is callback function; all
        non-empty arguments before it are concatenated to the
        LIST command.  (This *should* only be used for a pathname.)'''
        cmd = 'dir'
        func = None
        if args[-1:] and type(args[-1]) != type(''):
            args, func = args[:-1], args[-1]
        for arg in args:
            if arg:
                cmd = cmd + (' ' + arg)
        print cmd
        self.retrlines(cmd, func)

if __name__ == '__main__':
    f = FTP('ftp.ncbi.nih.gov')
    f.login()
    f.shim_dir('"blast"')
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top