import json
import socket
import time
import datetime
import sys


if __name__ == "__main__":
    ip = sys.argv[1]
    print(ip)
    time.sleep(60)
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((str(ip), 6666))

    client.send('{"s":{"sys.enable_hook_malloc": true}}\r'.encode('utf-8'))
    recv_data = client.recv(1024 * 24)
    while True:
        lctime = time.localtime(time.time())

        file_name = str(lctime.tm_year) + "_" + str(lctime.tm_mon) + "_" + str(lctime.tm_mday) + \
                    "_" + str(lctime.tm_hour) + "_" + str(lctime.tm_min)

        # 8640 次代表12小时
        count = 0
        while count < 8640:
            with open(file_name + ".json", 'a') as f:
                client.send('{"g":{"sys.thread_mem": true}}\r'.encode('utf-8'))
                recv_data = client.recv(1024 * 24)
                if not recv_data:
                    break

                tmp_json = json.loads(recv_data.decode('utf-8'))["g"]["sys.thread_mem"]
                print(tmp_json)
                f.write(json.dumps(tmp_json))
                f.write("\r")
                pass

            time.sleep(5)
            count += 1
            pass





