使用 urlopen 获取 FTP 服务器上文件的最后修改日期不起作用

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']

How can I get the last-modified time with python3 urllib? 之后,我想获取 FTP 服务器上文件的最后修改日期。但是,对于上面的代码,last-modifiedNone。有什么建议么?我希望解决方案适用于 python 2 和 python 3

documentation of urlopen 并未声称它 returns last-modified 用于 FTP 个 URL。

请注意 FTP 协议中没有 headers。 urlopen 只是伪造了一些 HTTP-like headers 以实现某种跨协议接口的兼容性。

要使用 FTP 检索时间戳,请使用 ftplib。参见 How to get FTP file's modify time using Python ftplib