import json
import socket
import pandas as pd
import numpy as np

timefrm = "%Y-%m-%d+%H:%M:%S"

def get_new_entry():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('localhost', 8888))
    s.sendall(b'{"set":{"type":"addr","addr":0,"val":15}}')

    response =json.loads(''.join(e.decode("utf-8") for e in s.recv(1024).split()[5:]))
    s.close()

    return response


if __name__ == '__main__':
    try:
        data = pd.read_csv("aggregate.csv")
    except:
        data = pd.DataFrame(pd.Series(get_new_entry())).T
    print(data)
    for i in range(10):
        data = data.append(pd.Series(get_new_entry()), ignore_index=True)

        print(i)

    # whitespace would be removed when transmitted
    data.systime = pd.to_datetime(data.systime, format=timefrm)
    data.to_csv("aggregate.csv", date_format=timefrm)
