gpt4 book ai didi

Python 请求 : download only if newer

转载 作者:太空狗 更新时间:2023-10-29 21:46:09 27 4
gpt4 key购买 nike

仅当服务器副本比本地副本更新时,从服务器下载新文件的标准 pythonic 方式是什么?

要么我的 python-search-fu 今天非常弱,要么确实需要像下面那样滚动自己的日期时间解析器和比较器。真的没有requests.header.get_datetime_object('last-modified')吗?或 request.save_to_file(url, outfile, maintain_datetime=True)

import requests
import datetime

r = requests.head(url)
url_time = r.headers['last-modified']
file_time = datetime.datetime.fromtimestamp(os.path.getmtime(dstFile))
print url_time #emits 'Sat, 28 Mar 2015 08:05:42 GMT' on my machine
print file_time #emits '2015-03-27 21:53:28.175072'

if time_is_older(url_time, file_time):
print 'url modtime is not newer than local file, skipping download'
return
else:
do_download(url)
os.utime(dstFile, url_time) # maintain server's file timestamp

def time_is_older(str_time, time_object):
''' Parse str_time and see if is older than time_object.
This is a fragile function, what if str_time is in different locale?
'''
parsed_time = datetime.datetime.strptime(str_time,
#Fri, 27 Mar 2015 08:05:42 GMT
'%a, %d %b %Y %X %Z')
return parsed_time < time_object

最佳答案

import requests
import datetime
from dateutil.parser import parse as parsedate
r = requests.head(url)
url_time = r.headers['last-modified']
url_date = parsedate(url_time)
file_time = datetime.datetime.fromtimestamp(os.path.getmtime(dstFile))
if url_date > file_time :
download it !

关于Python 请求 : download only if newer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29314287/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com