KSp
KSp

Reputation: 1239

How to store many dictionary and access it

I have a socket connection say client and server. So i am sending data from client to server and in server side, i am storing it as dictionary as key and value pair. But i am not sure how to stack up. For ex: I am sending a message 'animal' and i am storing it as my_dict={a:b} where a is the message and b is the ip address associated with it. So what i want is, if i send a message as 'human'. I want to store the message and ip address. but when i print my_dict it always gives the last message i sent. I actually want to print the whole set of dictionaries which was stored. i want to print 'animal':ip and 'human':ip.

I want to access this dictionary like assuming the animal as topics and ip as the connecting ip address for the communication. (Pub-sub)

server side:

    def bd_recv(host,port):
        my_dict=dict()
        #listening bd for other devices and controllers
        sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        address=host,port
        sock.bind(address)
        msg,client=sock.recvfrom(4089)
        a=msg.decode()
        b=client[0]
        my_dict={a:b}

        for key,value in my_dict.items():
            print (key)
    def pub-sub():
       context = zmq.Context()
       sub=context.socket(zmq.SUB)  # Note.
       #for the animal topic here i will refer to dictionary. if this animal topic is there i will take that key and value(ip) and append that to the variable ip here.
       sub.setsockopt_string(zmq.SUBSCRIBE, 'animal:')  # Note.
       ip=# here comes the value for animal which is ip.
       sub.connect('tcp://ip:8000')
       for i in range(2):
       print('Received: %s' % sub.recv())

    if __name__=="__main__":
        t1 = threading.Thread(target=bd_recv, name='broadcast_receive', args=(bd_of_the_machine, 9289))
        threads=threading.Thread(target=pub-sub)
        t1.start()
        threads.start()

Upvotes: 0

Views: 418

Answers (2)

qocu
qocu

Reputation: 405

On the server side you have to make a dict in scope that's outside the bd_recv and for example in bd_recv you have to return a dict {a:b} which you should add to that dict

Update:

import socket
import threading
import time

total_dict = {}

def bd_recv(host, port):
    global total_dict
    while True:
        my_dict=dict()
        #listening bd for other devices and controllers
        sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        address=host,port
        sock.bind(address)
        msg,client = sock.recvfrom(4089)
        a = msg.decode()
        b = client[0]
        my_dict = {a:b}
        total_dict.update({a:b})

        for key,value in my_dict.items():
            print (key)

def print_all_animals():
    while True:
        print(total_dict)
        time.sleep(1)

if __name__=="__main__":
    t1 = threading.Thread(
        target=bd_recv,
        name='broadcast_receive',
        args=("0.0.0.0", 9289)
    )
    t2 = threading.Thread(
        target=print_all_animals,
        name="print_all_animals",
        # args=()
    )
    t1.start()
    t2.start()

But I don't think using global variable is good solution but you didn't give any more context or mockup. In most scenarios queue with some kind of locking storage would be better and more "sure". It all depends on how do you want to access this dict in your code and you didn't provided that.

Upvotes: 0

Merig
Merig

Reputation: 2011

You are overwriting your my_dict variable everytime you receive a message, so only the last one will be saved. Try changing that line to

my_dict[a] = b

Also your dictionary need to be declared outside the function body:

my_dict = {}

def bd_recv(host,port):        
    #listening bd for other devices and controllers
    sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    address=host,port
    sock.bind(address)
    msg,client=sock.recvfrom(4089)
    a=msg.decode()
    b=client[0]
    my_dict[a] = b

    for key,value in my_dict.items():
        print (key)

Upvotes: 0

Related Questions