1

I'm trying to recive data from an API through ssl (the only allowed method), the dimension of "response" changes as the amount ("n_data") of requested data, so the socket buffer size is increased as the increment of data.
When I decode the response I find that it is incomplete (with bigger "n_data" values), and if I call "len(response)" it always returns max 13680 characters, ignoring missing characters and raising a JSON decode error.

request = json.dumps(request) + "\n"
connection.sendall(request.encode())

n_data = 5000
response = connection.recv(512*n_data).decode()
print(len(response))
response = json.loads(response)

Am I doing something wrong?

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • 2
    TCP is not message-based, but is simply a byte stream. If you know the exact length of the data to be received (usually through a header) then call `recv` with a reasonable size and accumulate data until you receive *exactly* that amount. Note that `if not data:` only works if the server closes the stream which why the loop hangs. – Mark Tolonen Mar 02 '23 at 18:49
  • 2
    Answers to [this question](https://stackoverflow.com/q/73064751/235698) should be informative. – Mark Tolonen Mar 02 '23 at 18:55
  • 1
    I'm afraid I don't understand the question. At all – user253751 Mar 02 '23 at 19:29
  • @user253751 Edited! (imagine my mental health) – Francesco Battisti Mar 02 '23 at 21:53
  • @FrancescoBattisti now I agree this the same problem that everyone else has: thinking that one send = one recv – user253751 Mar 03 '23 at 11:54

0 Answers0