如何在远程(Http)文件更改时执行操作? [英] How to perform an action when a remote (Http) file changed?

查看:193
本文介绍了如何在远程(Http)文件更改时执行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个脚本来检查URL,并在远程文件的Last-Modified标题发生更改时执行操作(下载+解压缩)。我想过用curl来获取头文件,但是我必须将它存储在每个文件的某个位置并执行日期比较。

有人使用(主要是)标准unix工具有不同的想法吗?



谢谢

解决方案

可能的解决方案是定期在客户端框中运行此算法。


  1. 创建一个HTTP请求,指明 If-Modified-Since 标题等于本地文件的日期。如果文件不存在,请不要包含这个头文件;
  2. 如果自从 If-Modified-Since 标头,或发送 304未修改 HTTP状态。

  3. 如果您收到 200 OK HTTP状态只需从HTTP主体获取有效内容并解压缩该文件。 如果另一方面收到 304 Not Modified 您知道您的文件是最新的。

  4. 使用 Last-Modified 标题触摸您的本地文件。这样您就可以与服务器的日期时间同步。

另一种方式是服务器推送通知例如)文件更改时。当收到通知时,客户端将执行上述算法。这意味着代码可以驻留在侦听文件系统更改的HTTP服务器中,然后将它们广播给感兴趣的人。

也许这个信息用于 curl 命令有一些重要性:


时间条件



HTTP允许客户指定时间
条件为
请求的文件。它是If-Modified-Since或
If-Unmodified-Since。 Curl允许你用
指定它们,用-z / - time-cond
标志。



例如,你可以很容易地用$仅当
远程文件比本地
副本更新时才会执行b $ b下载。它会变成这样:


curl -z local.html
http://remote.server.com/remote.html



或者你可以下载文件,只要
本地文件比远程
更新。通过在日期
字符串中加上' - '来做到这一点,例如:

curl -z -local.html
http://remote.server.com/remote.html



您可以指定一个自由文本日期作为
条件。如果昨天
以来更新,请告诉curl只下载
文件:



curl -z昨天
http://remote.server.com/remote.html



<然后curl将接受大范围的
日期格式。你总是让日期

的方式检查,并用短划线' - '预先加上它,并用短划线' - '作为前缀。

综上所述,您需要:


I want to create a script that checks an URL and perform an action (download + unzip) when the "Last-Modified" header of the remote file changed. I thought about fetching the header with curl but then I have to store it somewhere for each file and perform a date comparison.

Does anyone have a different idea using (mostly) standard unix tools?

thanks

解决方案

A possible solution would be periodically running this algorithm on the client box.

  1. Create a HTTP request indicating the If-Modified-Since header equal to the date of your local file. If the file does not exist yet do not include this header;
  2. The server will either send you the file if it was changed since the If-Modified-Since header in the payload or send 304 Not Modified HTTP status.
  3. If you receive a 200 OK HTTP status simply get the payload from the HTTP body and unzip the file.
  4. If in the other hand you received a 304 Not Modified you know that your file is up-to-date.
  5. Use the Last-Modified header to touch your local file. This way you will be in sync with the server datetime.

Another way would be for the server to push notifications (a broadcast package for example) when the file is changed. When the notification is received the client would then execute the above algorithm. This would imply code to live in the HTTP server that listens for file system changes and then broadcast them to interested parties.

Perhaps this info for the curl command is of some importance:

TIME CONDITIONS

HTTP allows a client to specify a time condition for the document it requests. It is If-Modified-Since or If-Unmodified-Since. Curl allow you to specify them with the -z/--time-cond flag.

For example, you can easily make a download that only gets performed if the remote file is newer than a local copy. It would be made like:

curl -z local.html http://remote.server.com/remote.html

Or you can download a file only if the local file is newer than the remote one. Do this by prepending the date string with a '-', as in:

curl -z -local.html http://remote.server.com/remote.html

You can specify a "free text" date as condition. Tell curl to only download the file if it was updated since yesterday:

curl -z yesterday http://remote.server.com/remote.html

Curl will then accept a wide range of date formats. You always make the date check the other way around by prepending it with a dash '-'.prepending it with a dash '-'.

To sum up, you will need:

这篇关于如何在远程(Http)文件更改时执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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