Search code examples
pythonftpurllibsix

Getting last modified date of file on FTP server using urlopen not working


from six.moves.urllib.request import urlopen, urlretrieve, urlrequest

conn = urlopen('ftp://ftp.cdc.noaa.gov/Datasets/ncep.reanalysis.dailyavgs/surface/lftx.sfc.2017.nc', timeout=20)
last_modified = conn.headers['last-modified']

Following How can I get the last-modified time with python3 urllib? I want to get last modification date of a file on a FTP server. However, with the code above, last-modified is None. Any suggestions? I want solution to work on both python 2 and python 3


Solution

  • The documentation of urlopen does not claim that it returns last-modified for FTP URLs.

    Note that there are no headers in FTP protocol. The urlopen just fakes few HTTP-like headers for some kind of compatibility of the interface across protocols.

    To retrieve timestamp using FTP, use the ftplib. See How to get FTP file's modify time using Python ftplib.