Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TypeError:使用python将字节存储到mongo db时_Python_Multithreading_Mongodb - Fatal编程技术网

TypeError:使用python将字节存储到mongo db时

TypeError:使用python将字节存储到mongo db时,python,multithreading,mongodb,Python,Multithreading,Mongodb,我正试图编写一个大型程序,在那里我进行服务器-客户机通信,一切正常,但当我试图将客户机数据存储到服务器端连接的Mongodb中时,我得到如下错误 错误: TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping 在客户端,我的数据类型显示为字符串,在服务器

我正试图编写一个大型程序,在那里我进行服务器-客户机通信,一切正常,但当我试图将客户机数据存储到服务器端连接的Mongodb中时,我得到如下错误

错误:

TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
在客户端,我的数据类型显示为字符串,在服务器端,当我打印类型时,它显示字节。但是为什么字节没有存储在数据库中呢。请发表你的想法,我也是python新手。 谢谢

服务器端:

import re
#getting ip address from machine and bd it to the client
class C():



          if __name__ == "__main__":
              ethernet_card = "wlp3s0"
              ip = ip1(ethernet_card)
          self.sock.bind((ip,4242))
          self.clients_list = []



    def talkToClient(self, ip):
            self.sock.sendto("ok".encode('utf-8'), ip)

    def listen_clients(self):
        #listen to multiple clients
            def parse(msg):
                uri = "mongodb://localhost:27017"
                client = pymongo.MongoClient(uri)
                database = client['orchtrail']
                collection = database['testData']
                result = database.testData.insert_many(msg)
                testData = collection.find({})
                for test in testData:
                    print(test)
                result.inserted_id
            while True:
                msg, client = self.sock.recvfrom(1024)
                print(type(msg))
                print(msg)
                parse(msg)

                print('connected with : ' + client[0]+ ':' + str(client[1]))
                t = threading.Thread(target= self.talkToClient,args=(client,))
                t.start()

if __name__=='__main__':
    C= C()
    C.broad(msg="")
    C.listen_clients()
客户端

temperature=""
hum=""
json=json.dumps({"temp"=temperature,"hum":hum})
msg=json
self.sock.sendto(msg.encode(),address)

您正在以下位置插入数据:

result = database.testData.insert_many(msg)
错误显示
msg
应该是
dict
,但是您写了您检查过的,并且它的类型是
byte

因此,您的问题是:如何将存储为
字节的
json
转换为
dict

您可以执行以下操作:

import pickle
import json

msg_as_dict = json.loads(pickle.loads(msg))

# check the type of msg_as_dict
print(type(msg_as_dict)) # should be <class 'dict'>

嘿,谢谢,顺便说一句,我刚开始工作。我也会把我的答案贴出来。@keerthana很高兴听到!顺便说一下,欢迎来到这个网站。一旦你觉得你的问题已经得到了回答,别忘了接受答案;)
result = database.testData.insert_many(msg_as_dict )