3

I write an application with two module.

  1. Agent : Collect information about computer resource and send to Manager.

  2. Manager : Listen data from client and write to database.

I has finished the collecting data functions on Agent. I write function for each kind of information. Exp : OS() for Operating System information, DISK() for Disk used information ....

Each function return a list contain information. I want to send those lists to Manager. But when send data to Manager i must change data type to string.

I think i should send each list one by one for me easy to change back the data type to list. I plan to write a for loop on Agent and Manager to send/receive data.

Agent :

resource_info = [OS(), CPU(), DISK(), MEMORY(), NETWORK(), FIREWALL()]

try:
    for x in resource_info:
        s.sendall(str(x))
except socket.error, e:
    print "Error sending data: %s" % e
    sys.exit(1)

# Close Socket
s.close()

Manager :

s.listen(10)
for x in range(0,4):
    if x == 0 :
        conn, addr = s.accept()
        OS = conn.recv(100000)
    if x == 1 :
        conn, addr = s.accept()
        DISK = conn.recv(100000)
    if x == 2 :
        conn, addr = s.accept()
        MEMORY = conn.recv(100000)
    ....

OS = OS.split(",")
DISK = DISK.split(",")
....

But i think it a silly way to do. Please give me some advices about how to send multi list on Agent to Manager. If you have exmple code, it would be great. Thanks so much, Quang

the.salman.a
  • 945
  • 8
  • 29
Park Yo Jin
  • 331
  • 1
  • 3
  • 15

1 Answers1

1

I would recommend that you create a dict that maps each resource to its corresponding info, instead of creating a list.

resource_info = {
  'OS': [...],
  'DISK': [...]
}

A cleaner way to send this data to a socket would be to use pickle. Using pickle, you can serialize Python objects.

import pickle
data = pickle.dumps({'foo': 'bar'})

On the manager, you can accept the bytes and de-serialize them using pickle.loads(...).

data = s.recv(4096)
deserialized_data = pickle.loads(data)

You can have a look at this answer for a sample implementation.

I would also recommend reading this blogpost to understand that sockets are byte-streams, not message streams.

xennygrimmato
  • 2,646
  • 7
  • 25
  • 47