Python / bash wget文件,查看内容是否与unix时间戳匹配

时间:2015-05-20 11:13:50

标签: python bash scripting timestamp

情景

一个Web服务器用户点击执行,这将生成一个名为now.txt的文件,在此文件中将显示当前的unix时间戳

在当前的机器上,我需要一种方法来检查该文件的时间戳是否在当前时间的5分钟内,如果执行另一个命令

我想在客户端计算机上每隔5分钟运行一次cronjob,它将从网络服务器执行wget文件,然后检查内容的unix时间戳,然后将其与当前时间

不确定这是否有意义并且不确定我是否过度了,所以最好得到一些建议?

Python脚本

wget file
check file is with 5 minutes of current time 
run another command




import wget
from datetime import datetime;

url = 'http://example.com/test/now.txt'
filename = wget.download(url) 

try:

        f = open("now.txt", "rb");
        age = datetime.utcnow() - datetime.utcfromtimestamp(long(f.read()));
except :
        pass

1 个答案:

答案 0 :(得分:0)

读取保存在远程文件中的Unix时间戳,并将其与客户端上的当前时间进行比较(假设时钟是同步的,例如,使用ntp):

#!/usr/bin/env python3
import time
from urllib.request import urlopen 

with urlopen('http://example.com/test/now.txt') as r:
   timestamp = int(r.read())
   if abs(time.time() - timestamp) < 300:
       print('the current time is within 5 minutes')