How to download the same file twice? (urlretrieve issue)
<p>Python 2.7.</p>

<pre><code>from urllib import urlretrieve
urlretrieve("ftp://ftp.wwpdb.org/pub/pdb/data/structures/divided/mmCIF/27/127d.cif.gz", "file1")
urlretrieve("ftp://ftp.wwpdb.org/pub/pdb/data/structures/divided/mmCIF/27/127d.cif.gz", "file2")
</code></pre>

<p>The first download goes correctly but the second fails:</p>

<pre><code>Traceback (most recent call last):
  File "C:/Jacek/Python/untitled/test2.py", line 3, in &lt;module&gt;
    urlretrieve("ftp://ftp.wwpdb.org/pub/pdb/data/structures/divided/mmCIF/27/127d.cif.gz", "file2")
  File "C:\Python27\lib\urllib.py", line 98, in urlretrieve
    return opener.retrieve(url, filename, reporthook, data)
  File "C:\Python27\lib\urllib.py", line 245, in retrieve
    fp = self.open(url, data)
  File "C:\Python27\lib\urllib.py", line 213, in open
return getattr(self, name)(url)
  File "C:\Python27\lib\urllib.py", line 558, in open_ftp
    (fp, retrlen) = self.ftpcache[key].retrfile(file, type)
  File "C:\Python27\lib\urllib.py", line 906, in retrfile
    conn, retrlen = self.ftp.ntransfercmd(cmd)
  File "C:\Python27\lib\ftplib.py", line 334, in ntransfercmd
    host, port = self.makepasv()
  File "C:\Python27\lib\ftplib.py", line 312, in makepasv
    host, port = parse227(self.sendcmd('PASV'))
  File "C:\Python27\lib\ftplib.py", line 830, in parse227
    raise error_reply, resp
IOError: [Errno ftp error] 200 TYPE is now 8-bit binary
</code></pre>

<p>On python 3 (with corresponding version of urlretrieve) this works as expected - both downloads succeeded.</p>

<p>Is it a way to resolve this on Python 2.7?</p>

<p>(Of course, you can say that downloading twice the same file doesn't make sense. I agree. I went into this problem while testing a module trying to download a file with different parameters (as it has nothing common with the problem, I simplified the example code) and I just was surprised with this strange behavior)</p>

<p>Unfortunately,urlretrieve has something wrong in Python2.7.
Called urlretrieve repeatedly works by HTTP, not by FTP in Python2.7.
The reason is ftplib sends PASV by ftplib again and again.
Fortunately,we can call urlcleanup before urlretrieve when download file by ftp.
And the documentation is <a href="https://docs.python.org/2/library/urllib.html" rel="nofollow">here</a>.</p>

python python-2.7