Get File Upload Time From Server

929 views Asked by At

Is there a way, using urllib2 or something else, to check the time a file was uploaded to a URL? Or even the time the file on the server side was last modified?

At the moment I'm manually using urllib2.urlopen() to read data from a url address. The arguments for the address change each day. What I'd like to do is figure out when each file was first available, so that I can pick the best time for the job to automatically run overnight.

1

There are 1 answers

0
Aditya On BEST ANSWER

The time is stored in the server which is usually sent to your browser as HTTP headers. You can access this in Javascript using document.lastModified property. Here's a solution in Python that reads headers and parses the information using regular expression and prints the result.

def get_upload_datetime(myurl):
    info = urllib2.urlopen(myurl).info() 
    datetime = re.search("Last-Modified: (.+)", str(info))
    if datetime:
        return datetime.groups()[0]

If you are also using contents of the webpage,use urlopen.info() and urlopen.read() on the same object (actually read only once) to avoid multiple fetches.

And if you want to do it manually, open webpage in the browser, open console (Ctrl+Shift+J) and type javascript:alert(document.lastModified). It should present an alert box with last modified time.