2

I want to send/recv data through socket.

I use python 2.7 because of ROS(melodic) and also use python 3.6 because of tensorflow.

The dict data, for example {'key_name':[[1,2,3],[4,5,6]]}, is sent and encoded with JSONEncoder.encoder().
The Client received the json data with loads() and resend it to the Server.
The Client uses python 2.7 and the Server uses python 3.6

def _send(socket, send_data):
    json_data = json.JSONEncoder().encode(send_data)
    socket.sendall(json_data)

def _recv(socket):
    recv_data = socket.recv(BUFSIZE)
    json_data = json.loads(recv_data, encoding="utf-8")

    return json_data

I have the error

  File "/usr/lib/python3.6/json/deoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 4097 (char 4096)

I need a python code which runs on both python 2.7 and 3.6.

2
  • How do you know the response is only 4096 bytes? Commented Apr 3, 2020 at 7:11
  • This answer stackoverflow.com/questions/61006696/… seems to confirm what @user207421 is suggesting. You should chech your json output, and make sure you arn't cutting any data off because BUFSIZE is too small. Commented Apr 3, 2020 at 7:19

1 Answer 1

3

The error is coming from json decoder.

json_data = json.loads(recv_data, encoding="utf-8"),

It is expected when your json data ( in your case the recv_data) is not in proper format. Usually missing a comma ',' or something. For debugging I would suggest you to dump the data received at server, before going for json.loads()

Sign up to request clarification or add additional context in comments.

3 Comments

You are right with what you are saying. I'm thinking it may not be malformed json, but instead BUFSIZE isn't big enough.
@Simon Yeah right. Dumping the output is the way to confirm
I tired your answer already but it didn't work for me. So I changed library msgpack. It looks good to me. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.