开发者

Test whether sync has happened (Python)

I have swf's which need to sync on a server which I have no access to. I can't know when this has happened by something on the server(there is a cron job which is supposed to run every 15 minutes, but it works... intermittently), so pretty much my only real option seems guess and check (wait 15 minutes... has it updated? Wait fifteen minutes...). A further annoyance is that sometimes it takes 5 minutes to see whether the version has updated or there is an older version. All told, this wastes a couple of hours a week, if not more.

I'm looking for a way for Python to let me know that the local file and t开发者_如何学Che file on the server are the same file.

My (completely untested) plan:

def ensureSynced(url,localfile,finaldest = None):
    urllib.request.urlretrieve(url,<download-file>)
    downloaded = '\n'.join(file(<download-file>).readlines())
    local      = '\n'.join(file(localfile).readlines())
    if finaldest is None: finaldest  = url
    if downloaded == local: # I believe this will work?
        webbrowser.open(finaldest)
    else
        time.sleep(90)
        ensureSynced(url,localfile,finaldest)

But, I can't help but thinking that there must be a better way.

THE REAL QUESTION

Is there a way to have Python easily check whether a resource on a remote server is the same as the local resource if there is no FTP availability? OR Is there a better way to accomplish the above code?

Side Note

No, I don't have the authority to fix it. I am trying to get it so that the server simply outputs "sync-time" somewhere in the HTML -- even in a comment -- but my pleas have largely fallen on "it isn't a priority" ears.


I would suggest checking whether the target server supports HEAD requests and whether the etag header and last modified header are set.

If this is the case, you could do (even every x seconds) a head request and compare the etag with an md5sum of a local file and if the last modified time is correct than you can see what time the file was synced.

For sending a HEAD request, see for example How do you send a HEAD HTTP request in Python 2?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜