我有下面的代码,它只是下载一个文件并保存它。我想每 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它仅请求 header 而不下载正文(即文件内容),然后检查 Last Modified header 的值是否已更改。
例如
resp = http.head(($xmlServerPath+"levels.xml")
last_modified = resp['last-modified']
if last_modified != previous_last_modified
# file has changed
end
关于ruby - 在 Ruby 中下载远程文件之前如何获取它的 mtime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1509063/