查了很多资料,都说是因为FTP主被动模式的问题,FTP协议有两种工作方式:PORT方式和PASV方式,中文意思为主动式和被动式。但是没说在python ftplib里如何转成被动模式。
在使用python ftplib进行上传下载,报500 Illegal PORT command错误时,需要把模式改成PASV。
python中ftplib需要在调用上传/下载之前,设置PASV模式ftp.set_pasv(True)
,代码放置的位置如下:
def upload_file(self, local_file, remote_file):
buf_size = 1024
file_handler = open(local_file, 'rb')
self.ftp.set_pasv(True)
self.ftp.storbinary('STOR %s' % remote_file, file_handler, buf_size)
file_handler.close()
self.debug_print('上传: %s' % local_file + "成功!")