c0d3rz
c0d3rz

Reputation: 679

Getting servers date using FTP

I'm trying to get the date modified time of a file created on an FTP server using Python, I am currently using the ftputil external library and doing the following:

file_folder_time_stamp = time.strftime('%y%m%d%H%M%S', time.gmtime(host.path.getmtime(dir_or_file)))

Unfortunately, the return time I get is for the year 2013. Instead of 2014. I've made sure the server running the FTP server is updated to the current time. Doing a date command on the terminal, ensures I get the right time. Am I just parsing something incorrectly then?

I create a folder, and check the time of creating using the getmtime command, and it still returns a string such as 13090810000. This should instead look like 140908xxxx

Upvotes: 1

Views: 1109

Answers (1)

xbello
xbello

Reputation: 7443

What epoch-time do you get from host.path.getmtime(dir_or_file)? What do you get from time.gmtime(...)? To check time differences you can substract two dates to get a datetime.timedelta object, and probably save some troubles with transformations:

from datetime import datetime
from ftputil import FTPHost

URL = "ftp.sample.org"

with FTPHost(URL, "anonymous", "[email protected]") as host:
    mod_time = host.path.getmtime("robots.txt")

    dif = datetime.now() - datetime.fromtimestamp(mod_time)

    print dif.seconds

Upvotes: 4

Related Questions