2008-10-16 72 views
1

我需要一個非常具體的(非標)的字符串發送到FTP服務器:高級Python FTP - 我可以控制ftplib如何與服務器通話嗎?

dir "SYS:\IC.ICAMA." 

的情況下是至關重要的,因爲是報價和其內容的風格。

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

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

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

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

回答

4

請嘗試以下操作。它是對原始FTP.dir命令的修改,它使用「dir」而不是「LIST」。它給我測試過的ftp服務器提供了一個「DIR未知」的錯誤,但它確實發送了你之後的命令。 (你會想刪除我用來檢查的打印命令。)

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"') 
+0

謝謝 - 明天我可以測試一下:-) – 2008-10-16 22:57:46

相關問題