ruby - 在 Ruby 中下载远程文件之前如何获取它的 mtime?

标签 ruby http file download

我有下面的代码,它只是下载一个文件并保存它。我想每 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/

相关文章:

ruby-on-rails - 在站点之间共享 session 数据?

ruby - Cygwin 工具与 Linux 终端有何不同? Cygwin 是否有任何缓慢问题

ruby - I18n : how can I guard against empty interpolation arguments?

python-3.x - 包装io.BufferedIOBase,使其变为可搜索

string - 用于 HTTP 的 Lua 解析器,无法找到字符串结尾

android - SD卡的Kitkat规则和限制如何处理?

从 C 中的 CSV 文件计算值的平均值

ruby - 为什么找不到库: no such file to load

HttpSendHttpResponse 没有发送数据

windows-7 - 如何在 Windows 7 中使用 C++ 保存文件