Python ftplib 上传失败,出现错误 421

Python ftplib uploads failing with error 421

我目前正在尝试使用 Python 的 ftplib 库将文件上传到 ftp 服务器。该文件相对较小 (~400MB),但我的脚本总是以同样的方式崩溃:

total_size = 492917709
Traceback (most recent call last):
  File "ftpmule.py", line 83, in <module>
    dest_ftp.storbinary('RETR %s' % base_fn, fhandle, 1024)
  File "/usr/lib/python2.6/ftplib.py", line 233, in voidresp
    resp = self.getresp()
  File "/usr/lib/python2.6/ftplib.py", line 266, in getresp
    raise error_temp, resp
ftplib.error_temp:  421 Data timeout. Reconnect. Sorry.

相关代码:

dest_ftp = FTP(ftp_dest_host)
dest_ftp.login(ftp_user, ftp_pass)
dest_ftp.cwd(ftp_dest_path)

filename = "file.zip"

with open(filename, 'rb') as fhandle:
    dest_ftp.storbinary('RETR %s' % filename, fhandle, 1024)

根据您的意见,服务器不支持被动模式。 Python ftplib 自 2.2 起默认使用被动模式,因此您必须使用 set_pasv(False):

明确禁用它
dest_ftp = FTP(ftp_dest_host)
dest_ftp.login(ftp_user, ftp_pass)
dest_ftp.cwd(ftp_dest_path)
dest_ftp.set_pasv(False)

filename = "file.zip"

with open(filename, 'rb') as fhandle:
    dest_ftp.storbinary('RETR %s' % filename, fhandle, 1024)