从 ftp 下载第二个文件失败

Downloading second file from ftp fails

我想从 python 中的 FTP 下载多个文件。我的代码在我只下载 1 个文件时有效,但对多个文件无效!

import urllib
urllib.urlretrieve('ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_package/00/00/PMC1790863.tar.gz', 'file1.tar.gz')
urllib.urlretrieve('ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_package/00/00/PMC2329613.tar.gz', 'file2.tar.gz')

错误说:

Traceback (most recent call last):
  File "/home/ehsan/dev_center/bigADEVS-bknd/daemons/crawler/ftp_oa_crawler.py", line 3, in <module>
    urllib.urlretrieve('ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_package/00/00/PMC2329613.tar.gz', 'file2.tar.gz')
  File "/usr/lib/python2.7/urllib.py", line 98, in urlretrieve
    return opener.retrieve(url, filename, reporthook, data)
  File "/usr/lib/python2.7/urllib.py", line 245, in retrieve
    fp = self.open(url, data)
  File "/usr/lib/python2.7/urllib.py", line 213, in open
    return getattr(self, name)(url)
  File "/usr/lib/python2.7/urllib.py", line 558, in open_ftp
    (fp, retrlen) = self.ftpcache[key].retrfile(file, type)
  File "/usr/lib/python2.7/urllib.py", line 906, in retrfile
    conn, retrlen = self.ftp.ntransfercmd(cmd)
  File "/usr/lib/python2.7/ftplib.py", line 334, in ntransfercmd
    host, port = self.makepasv()
  File "/usr/lib/python2.7/ftplib.py", line 312, in makepasv
    host, port = parse227(self.sendcmd('PASV'))
  File "/usr/lib/python2.7/ftplib.py", line 830, in parse227
    raise error_reply, resp
IOError: [Errno ftp error] 200 Type set to I

我该怎么办?

这是 python 2.7 中 urllib 中的错误。举报了here. The reason behind the same is explained here

Now, when a user tries to download the same file or another file from same directory, the key (host, port, dirs) remains the same so open_ftp() skips ftp initialization. Because of this skipping, previous FTP connection is reused and when new commands are sent to the server, server first sends the previous ACK. This causes a domino effect and each response gets delayed by one and we get an exception from parse227()

一个可能的解决方案是清除可能由以前的调用建立的缓存。您可以在 urlretrieve 调用之间使用 urllib.urlcleanup() 方法调用,如前所述 here.

希望对您有所帮助!