我正在尝试在 python 2.7 中自动化测试(在 linux ubuntu 12.04 上的 eclipse 中),并且我需要测试 FTP 服务器如下: 我必须在主机上创建一个文件,通过 FTP 将其传输到另一台电脑,然后以另一个名称将其传输回我的主机。然后我有两个文件需要比较。我是这样开始的:
#create the two files
firstFilename = "first_testfile.txt"
secondFilename = "second_testfile.txt"
os.system("echo \"test\" > {0}".format(firstFilename))
os.system("touch {0}".format(secondFilename))
#setup FTP connection transfer file to other computer
ftp = self.setupFTP()
ftp.set_pasv(True)
f = open(firstFilename)
ftp.storbinary("STOR {0} ".format(firstFilename), f)
f.close
ftp.quit()
#setup FTP connection transfer file back to host (other filename)
ftp = self.setupFTP()
ftp.set_pasv(True)
f = open(secondFilename, "wb")
ftp.retrbinary("RETR {0} ".format(secondFilename), f.write)
ftp.quit()
#comparison-part will be implemented later
firstOutput = os.system("cat {0}".format(firstFilename))
secondOutput = os.system("cat {0}".format(secondFilename))
#compare somehow
执行此代码会出现以下错误:
ftp.retrbinary("RETR {0} ".format(secondFilename), f.write)
error_perm: 550 second_testfile.txt : No such file or directory
有人知道我做错了什么吗?
最佳答案
编辑并解决:我发现了我的问题,它应该是
ftp.retrbinary("RETR {0} ".format(firstFilename), f.write)
而不是
ftp.retrbinary("RETR {0} ".format(secondFilename), f.write)
因为我认为 RETR 之后我必须告诉应该如何调用该文件(实际上文件处理程序就是用于此目的),而不是如何调用 FTP 上的文件。
关于Python ftplib 文件传输 error_perm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20048682/