[Tutor] How to update file?

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 11 Jan 2002 20:52:52 -0800 (PST)


On Fri, 11 Jan 2002, A wrote:

> I need to update a local file from a web server if the file on this
> web server is newer then that on my local hard disk. Do you have any
> idea how it can be done?

I'm not sure if one can get a guaranteed 'last-modified' time of a web
page.  I did a quick search, and found something:

    http://www.xav.com/scripts/search/help/1068.html

The page suggests that we look at the file itself, and see if it has a
'last-modified' META header.  But that's a somewhat sketchy way of doing
it... *grin*

Alternatively, we can try to look at the "last-modified" header that gets
sent to us by the web server.  There's a description of this optional
header from the W3 folks:

    http://www.w3.org/Protocols/HTTP/Object_Headers.html#last-modified

We can use the 'httplib' module to grab at this header.


Here's a small program that'll get the last-modified header if the web
server supports it:

###
import httplib
import urlparse

def getLastModifiedDate(url):
    url_pieces = urlparse.urlparse(url)
    base, path = url_pieces[1], url_pieces[2]     ## this can be
                                                  ## improved...
    conn = httplib.HTTPConnection(base)
    conn.request("GET", path)
    response = conn.getresponse()
    return response.getheader('last-modified')
###


We'd better test this, just to make sure it works!  *grin*

###
>>> getLastModifiedDate("http://python.org")
'Fri, 11 Jan 2002 03:49:53 GMT
###


However, maybe a simpler approach might work: how about just checking if
your local file is different from what's on the web server?


Hope this helps!