From 4db6e9d330f05e9eaaa323622b824311b346a5d6 Mon Sep 17 00:00:00 2001 From: lupzhu Date: Thu, 9 Jan 2020 15:30:16 +0800 Subject: [PATCH] fix last line is incomplete --- tools/hijack.py | 4 ++-- yabgp/handler/default_handler.py | 34 ++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/tools/hijack.py b/tools/hijack.py index e548142..76e0560 100644 --- a/tools/hijack.py +++ b/tools/hijack.py @@ -77,7 +77,7 @@ cfg.BoolOpt('hijack', default=True, help='hijack'), - cfg.IntOpt('num',default=10, + cfg.IntOpt('num', default=10, help='new next hop address'), ] @@ -167,7 +167,7 @@ def send_update(): message_count -= 1 continue - if message_count>= start_number and message_count < start_number+ 100: + if message_count >= start_number and message_count < start_number + CONF.attribute.num: message = message_json['msg'] diff --git a/yabgp/handler/default_handler.py b/yabgp/handler/default_handler.py index e685a6d..dcaed4f 100644 --- a/yabgp/handler/default_handler.py +++ b/yabgp/handler/default_handler.py @@ -97,16 +97,30 @@ def get_last_seq_and_file(msg_path): file_list.sort() msg_file_name = file_list[-1] try: - with open(msg_path + msg_file_name, 'r') as fh: - line = None - for line in fh: - pass - last = line - if line: - if last.startswith('['): - last_seq = eval(last)[1] - elif last.startswith('{'): - last_seq = json.loads(last)['seq'] + with open(msg_path + msg_file_name, 'rb') as fh: + offset = -500 + break_flag = False + while True: + fh.seek(offset, 2) + lines = fh.readlines() + if len(lines) >= 5: + for i in range(len(lines)-1, 0, -1): + last = lines[i] + try: + if last.startswith('['): + last_seq = eval(last)[1] + break_flag = True + break + elif last.startswith('{'): + last_seq = json.loads(last)['seq'] + break_flag = True + break + except Exception as e: + LOG.error(e) + LOG.error(last) + if break_flag: + break + offset *= 2 except OSError: LOG.error('Error when reading bgp message files') except Exception as e: