P
P
pcdesign2018-09-04 10:49:05
Python
pcdesign, 2018-09-04 10:49:05

How to compare Last-Modified with http header and file last modification time?

>>> 
>>> r = requests.get('https://ya.ru')    
>>> 
>>> 
>>> r.headers['Last-Modified']
'Tue, 04 Sep 2018 07:46:09 GMT'

And file
touch /tmp/1.txt
How to compare file mtime with r.headers['Last-Modified'] and understand who is older?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pcdesign, 2018-09-04
@pcdesign

>>> import datetime
>>> dt = parse(r.headers['Last-Modified'])
>>> dt_header = dt.replace(tzinfo=None)       
>>> mtime = os.path.getmtime('/tmp/1.txt')        
>>> dt_file = datetime.datetime.fromtimestamp(mtime)
>>> if dt_file > dt_header:             
...     print(1)
... 
1

D
dollar, 2018-09-04
@dollar

Convert to a single format.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question