python - 如何解决 Json 错误:TypeError:需要类似字节的对象,而不是“str”
问题描述
请使用正确的语法 pythoh 代码更正导致错误的代码:
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data)
错误是:
json_data 处的 self.connection.send(json_data )是
TypeError:需要一个类似字节的对象,而不是“str”
解决方案
您需要在发送前对数据进行编码,这应该是解决方案
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data.encode())