Python ftplib上传失败,错误421

时间:2016-12-02 16:03:07

标签: python ftplib

我目前正在尝试使用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)

1 个答案:

答案 0 :(得分:1)

从您的评论中,服务器不支持被动模式。默认情况下,Python ftplib默认使用被动模式,因此您必须使用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)