在Ruby下载之前如何获取远程文件的mtime? [英] How to get a remote-file's mtime before downloading it in Ruby?

查看:112
本文介绍了在Ruby下载之前如何获取远程文件的mtime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,它只是下载一个文件并保存。我想每30秒运行一次,并检查远程文件的mtime是否已更改,如果有的话,下载它。
我将创建一个线程,每次迭代之后休眠30秒,为此无限循环,但是;如何检查远程文件的mtime而不下载?

  Net :: HTTP.start($ xmlServerHostname){| http | 
resp = http.get($ xmlServerPath +levels.xml)
open(levels.xml,w){| file |
file.write(resp.body)
}
}


解决方案

在您执行 http.get 之前,请执行 http.head ,它只需要下载头文件(即文件内容),然后检查最后修改标题的值已更改。



例如

  resp = http.head(($ xmlServerPath +levels.xml)
last_modified = resp ['last-modified']
如果last_modified!= previous_last_modified
#文件已更改
结束


I have the below code, which simply downloads a file and saves it. I want to run it every 30 seconds and check if the remote-file's mtime has changed and download it if it has. I'll be creating a thread which sleeps 30seconds after every iteration of an endless loop for that purpose, but; how do I check a remote file's mtime without downloading it?

Net::HTTP.start($xmlServerHostname) { |http|
                resp = http.get($xmlServerPath+"levels.xml")
                open("levels.xml", "w") { |file|
                    file.write(resp.body)
                }
            }

解决方案

Before you do your http.get do an http.head which requests just the headers without downloading the body (i.e. the file contents) then check if the value of the Last Modified header has changed.

e.g.

resp = http.head(($xmlServerPath+"levels.xml")
last_modified = resp['last-modified']
if last_modified != previous_last_modified
  # file has changed
end

这篇关于在Ruby下载之前如何获取远程文件的mtime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆