簡體   English   中英

高級Python FTP - 我可以控制ftplib與服務器的對話方式嗎?

[英]Advanced Python FTP - can I control how ftplib talks to a server?

我需要向FTP服務器發送一個非常具體的(非標准)字符串:

dir "SYS:\IC.ICAMA."

案例是至關重要的,報價的風格和內容也是如此。

不幸的是,ftplib.dir()似乎使用'LIST'命令而不是'dir'(並且它使用了錯誤的情況用於此應用程序)。

FTP服務器實際上是一個電話交換機,它是一個非常非標准的實現。

我嘗試使用ftplib.sendcmd(),但它也發送'pasv'作為命令序列的一部分。

有沒有簡單的方法向FTP服務器發出特定命令?

請嘗試以下方法。 它是原始FTP.dir命令的修改,它使用“dir”而不是“LIST”。 它在我測試它的ftp服務器上給出了“DIR not understand”錯誤,但它確實發送了你所追求的命令。 (您將要刪除我用來檢查的打印命令。)

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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM