#!/usr/bin/python

import socket

def fetch_msg_main():
    HOST = "livebgp.netsec.colostate.edu"
    HOST = "bgpdata1.netsec.colostate.edu"
    PORT = 50001
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((HOST, PORT))
    data = sock.recv(1024)
    print repr(data)
    while True:
        data = sock.recv(1024)
        while len(data) != 0:
            yield data[0]
            data = data[1:]

def fetch_msg():
    END_STR = "</BGP_MESSAGE>"
    msg = ""
    match_count = 0
    i = fetch_msg_main()
    while True:
        ch = i.next()
        msg = msg + ch
        if ch == END_STR[match_count]:
            match_count += 1
            if match_count == len(END_STR):
                yield msg
                msg = ""
                match_count = 0
        else:
            match_count = 0


msg_i = fetch_msg()

while True:
  print "--"
  print msg_i.next()
