1
0
Fork 0
mirror of https://github.com/sileht/bird-lg.git synced 2024-11-22 06:54:43 +01:00

Add AS name persistant cache and reenable it

This commit is contained in:
Mehdi Abaakouk 2012-05-28 13:51:16 +02:00
parent 820e3860ef
commit 1a105698c5
2 changed files with 20 additions and 3 deletions

7
lg.py
View file

@ -26,7 +26,7 @@ from urllib2 import urlopen
from urllib import quote, unquote from urllib import quote, unquote
import json import json
from toolbox import mask_is_valid, ipv6_is_valid, ipv4_is_valid, resolve from toolbox import mask_is_valid, ipv6_is_valid, ipv4_is_valid, resolve, save_cache_pickle, load_cache_pickle
import pydot import pydot
from flask import Flask, render_template, jsonify, redirect, session, request, abort, Response from flask import Flask, render_template, jsonify, redirect, session, request, abort, Response
@ -257,9 +257,9 @@ def show_route_for(hosts, proto):
def show_route_for_detail(hosts, proto): def show_route_for_detail(hosts, proto):
return show_route("prefix_detail", hosts, proto) return show_route("prefix_detail", hosts, proto)
ASNAME_CACHE = {} ASNAME_CACHE_FILE = "/tmp/asname_cache.pickle"
ASNAME_CACHE = load_cache_pickle(ASNAME_CACHE_FILE, {})
def get_as_name(_as): def get_as_name(_as):
return "AS%s" % _as
if not ASNAME_CACHE.has_key(_as): if not ASNAME_CACHE.has_key(_as):
whois_answer = whois_command("as%s" % _as) whois_answer = whois_command("as%s" % _as)
as_name = re.search('as-name: (.*)', whois_answer) as_name = re.search('as-name: (.*)', whois_answer)
@ -267,6 +267,7 @@ def get_as_name(_as):
ASNAME_CACHE[_as] = as_name.group(1).strip() ASNAME_CACHE[_as] = as_name.group(1).strip()
else: else:
ASNAME_CACHE[_as] = _as ASNAME_CACHE[_as] = _as
save_cache_pickle(ASNAME_CACHE_FILE, ASNAME_CACHE)
if ASNAME_CACHE[_as] == _as: if ASNAME_CACHE[_as] == _as:
return "AS%s" % _as return "AS%s" % _as
else: else:

View file

@ -21,6 +21,7 @@
from dns import resolver from dns import resolver
import socket import socket
import pickle
def resolve(n, q): def resolve(n, q):
@ -49,5 +50,20 @@ def ipv6_is_valid(n):
except socket.error: except socket.error:
return False return False
def save_cache_pickle(filename, data):
output = open(filename, 'wb')
pickle.dump(data, output)
output.close()
def load_cache_pickle(filename, default = None):
try:
pkl_file = open(filename, 'rb')
except IOError:
return default
try:
data = pickle.load(pkl_file)
except:
data = default
pkl_file.close()
return data