【发布时间】:2017-01-12 04:04:30
【问题描述】:
下面是一个代码 sn-p,它根据服务器和本地之间的最后修改日期下载文件。
try:
url = "https://10.10.10.10/version.ini"
local_ini = ".//config/version.ini"
filetime = (time.strftime('%a, %d %b %Y %X GMT', time.gmtime(os.path.getmtime(local_ini))))
print("File Last Modified: {0}".format(filetime))
r = requests.get(url,stream=True)
meta = r.headers['last-modified']
logging.info("Web Last Modified: {0}".format(meta))
if filetime < meta:
print("Newer file found! Downloading...")
if r != False:
try:
fileName = os.path.basename(urlparse.urlsplit(r.url)[2])
with open(".//config/version.ini"e, 'wb') as f:
shutil.copyfileobj(r.raw,f)
finally:
r.close()
else:
logging.info('No new version found. You got the latest file!')
except requests.exceptions.RequestException as e:
logging.info(e)
当我在同一天修改文件时,我得到了正确的结果:
File Last Modified: Thu, 12 Jan 2017 03:42:23 GMT
Web Last Modified: Thu, 12 Jan 2017 03:44:22 GMT
Newer file found! Downloading...
但是当我修改一个文件时,一天后我得到了错误的结果:
File Last Modified: Wed, 11 Jan 2017 08:38:38 GMT
Web Last Modified: Thu, 12 Jan 2017 02:44:45 GMT
No new version found. You got the latest file!
为什么时间戳比较会给出错误的结果?
【问题讨论】:
标签: python datetime time python-requests