我需要向 FTP 服务器发送一个非常特定(非标准)的字符串:
dir "SYS:\IC.ICAMA."
案例至关重要,引文的风格及其内容也很重要。
不幸的是, ftplib.dir() 似乎使用了 'LIST' 命令而不是 'dir' (并且它为此应用程序使用了错误的大小写)。
FTP 服务器实际上是一个电话交换机,它是一个非常不标准的实现。
我尝试使用 ftplib.sendcmd(),但它也将“pasv”作为命令序列的一部分发送。
是否有向 FTP 服务器发出特定命令的简单方法?
试试下面的。它是对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"')