Although you do not show how the socket is created, by referring to the sendall()
method on the other side, it follows that it is a TCP type socket.
You also do not mention which version of python you are using, but since you initialize datos
with a string ( ""
) and not with a string of bytes ( b""
) I deduce that it is Python2.
Your program has a bug, which in Python3 would have been a runtime error that would have helped you detect the problem, and that is that you are comparing a string with an integer. In python 3 such a comparison would be prohibited and would generate the exception TypeError: unorderable types: str() < int()
, but in Python2 it is legal and produces False
*.
In particular, the problematic line is this:
if parte < buff_size:
Where parte
is a string and buff_size
is an integer. I assume you wanted to say if len(parte) < buff_size
.
Another way to implement your loop would be:
def recibirTodo(sock):
datos = ""
buff_size=4096
recibidos = "aun nada"
while recibidos:
recibidos = sock.recv(buff_size)
datos += recibidos
return datos
Which works because by the time you have finished receiving all the data (and the socket has been closed from the other end), recv()
will return the empty string. Remember that the other end must close the connection. If not, there is no way you can know if there is or is not more data unless you have a protocol of your own that, for example, first sends the number of bytes that the next message will have.
*: because the way in which Python 2 solves the comparison of data between different types is to flip , and what it does is to compare its types alphabetically, and it turns out that "str"
is greater than "int"