如何存储多个字典并访问它们

2024-04-25 16:37:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个套接字连接说客户端和服务器。所以我将数据从客户机发送到服务器,在服务器端,我将数据作为字典存储为密钥和值对。但我不知道该怎么做。 例如:我正在发送一条消息'animal',并将其存储为我的dict={a:b},其中a是消息,b是与之相关的ip地址。所以我想要的是,如果我以“人类”的身份发送信息。我想存储消息和ip地址。但当我打印我的字典时,它总是给出我最后一条信息。实际上我想打印存储的整套词典。我想打印“动物”:ip和“人类”:ip。你知道吗

我想访问这个字典,就像假设动物是主题,ip是通信的连接ip地址一样。(出版分公司)

服务器端:

    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()

Tags: andthekeyip消息for字典my
2条回答

在服务器端,您必须在bd_recv之外的范围内生成dict,例如在bd_recv中,您必须返回一个dict{a:b},您应该将它添加到该dict中

更新:

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()

但我不认为使用全局变量是一个好的解决方案,但你没有给出更多的上下文或模型。在大多数情况下,使用某种锁定存储的队列会更好、更“确定”。这完全取决于您希望如何在代码中访问这个dict,而您没有提供它。你知道吗

每次收到消息时都会覆盖my_dict变量,因此只保存最后一个变量。试着把那行改成

my_dict[a] = b

您的字典也需要在函数体之外声明:

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)

相关问题 更多 >