我的客户端中有以下代码:
data = {"method": 2,"read": 3}
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(server_address)
req = json.dumps(data)
s.send(req)
我正在服务器中尝试以下操作:
# Threat the socket as a file stream.
worker = self.conn.makefile(mode="rw")
# Read the request in a serialized form (JSON).
request = worker.readline()
result = json.loads(request)
print(result)
我收到No JSON object could be decoded
错误。我正在使用python 3.3。我无法理解我的错误在哪里,似乎send方法不发送json对象。有什么想法吗?
Edit
:我修复了JSON格式,现在问题是服务器上的<class 'TypeError'>: unsupported operand type(s) for +: 'NoneType' and 'str'
和客户端上的s.send(req)
TypeError: 'str' does not support the buffer interface
答案 0 :(得分:1)
我假设您正在使用python 3,从错误判断。
您需要将数据编码为字节。套接字不能直接发送python3字符串。
答案 1 :(得分:0)
您的JSON无效。通过JSON lint来查找错误。 http://jsonlint.com/
答案 2 :(得分:0)