hadiasghari / pyasn

Python IP address to Autonomous System Number lookup module. (Supports fast local lookups, and historical lookups using archived BGP dumps.)
Other
292 stars 72 forks source link

pyasn_util_download --latest fails between 12AM and xxx #1

Closed hadiasghari closed 9 years ago

hadiasghari commented 10 years ago

The --latest switch downloads the RIB file for today. However, in the early hours of the day in a time zone ahead of the server, the server has not yet created any files for that day. So the script aborts with "NOT FOUND" error, instead of downloading anythig.

A simple good alternative would be to just download the last RIB file available; another is to change the time to the RouteView server's time zone, and go back a day if necessary

khamin commented 9 years ago

This is how I implemented latest RIB downloading:

# -*- coding: utf-8 -

import sys
import ftplib

DOMAIN = 'archive.routeviews.org'

def download_latest ():
    print 'Connecting to %s' % DOMAIN
    ftp = ftplib.FTP(DOMAIN)

    print 'Trying to log in as anonymous...'
    ftp.login()

    print 'Defining latest RIB file...'

    ftp.cwd('/'.join((
        max(ftp.nlst('bgpdata')),
        'RIBS',
    )))

    filename = max(ftp.nlst())
    print 'Downloading %s' % filename

    with open(filename, 'wb') as fp:
        def recv (s):
            fp.write(s)
            recv.chunk += 1

            if recv.chunk % 30 == 0:
                sys.stdout.write('.')
                sys.stdout.flush()

        recv.chunk = 0
        ftp.retrbinary('RETR %s' % filename, recv)

    ftp.close()
    print '\n', 'Download complete'

Feel free to use it.

hadiasghari commented 9 years ago

Thank you so much, I've added it to the main script