python ftp and get recent file

the following is a script which downloads the latest image from  one of the NASA ftp server at eol.jsc.nasa.gov and to my amazement there are new images every 5 mins!, so I sent python to grab that image….

python supports ftp by importing the ftplib module and ftplib.FTP class.

connecting to an ftp server is very straightforward…

1
2
3
4
5
>>>from ftplib import FTP
>>>f = FTP('my ftp server ')
>>>f.login('anonymous', 'your@email.address')
:
>>>f.quit()

The core job inside in this script is to first dump the listing of files into a list, then reading that list line by line, split the date and convert it to a datetime object and store it in a new list, also i created another list which stores the filenames, next i combined these two lists to form a dictionary, and sorted the dictionary using the KEY(datetime object) in reverse order, and ran the loop only once as i need only the first image.

here is the full script..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python

import ftplib
import os
import socket
import sys
import time
import datetime

HOST = 'eol.jsc.nasa.gov'


def main():
    try:
        f = ftplib.FTP(HOST)
    except (socket.error, socket.gaierror), e:
        print 'cannot reach to %s' % HOST
        return
    print "Connect to ftp server"

    try:
        f.login('anonymous','alpha@krisindigitalage.com')
    except ftplib.error_perm:
        print 'cannot login anonymously'
        f.quit()
        return
    print "logged on to the ftp server"


    data = []
    f.dir(data.append)
    datelist = []
    filelist = []
    for line in data:
        col = line.split()
        datestr = ' '.join(line.split()[0:2])
        date = time.strptime(datestr, '%m-%d-%y %H:%M%p')
        datelist.append(date)
        filelist.append(col[3])

    combined = zip(datelist,filelist)
    new_dict = dict(combined)
#    print who
#    for key,value in sorted(new_dict.iteritems(), key =lambda (k,v): (v,k)):

    for key in sorted(who.iterkeys(), reverse=True):
       print "%s: %s" % (key,new_dict[key])
       filename = new_dict[key]
       print "file to download is %s" % filename
       try:
           f.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
       except ftplib.err_perm:
           print "Error: cannot read file %s" % filename
           os.unlink(filename)
       else:
           print "***Downloaded*** %s " % filename
       return

    f.quit()
    return


if __name__ == '__main__':
    main()
 

Wow. It's Quiet Here...

Be the first to start the conversation!

Leave a Reply:

Gravatar Image

*

Theme Tweaker by Unreal