What differences do the sendall & send functions have in python?

1

In Python, in the module socket there are two functions: send() and sendall() , both have the same parameters and the documentation of both is quite similar that I do not fully understand what differences there are between one and another one since at least for me it seems that they do the same thing.

from socket import *

s=socket(AF_INET,SOCK_STREAM)
s.bind(("",9000))
s.listen(5)

while True:
    c,a=s.accept()
    print "servidor \n"
    print "recived connection from ", a
    dat=c.recv(1000)
    c.sendall("hello %s\n"%a[0])
    c.send("hola")
    print c.recv(1000)
    c.close()
    
asked by djarte 25.07.2018 в 22:50
source

1 answer

1

I understand that we are talking about the 2x branch of Python, go to the documentation of the module socket :

  • socket.send(string[, flags]) : Send data to the socket. The socket must be connected to a remote socket. The optional argument flags has the same meaning as the previous one for recv (). Returns the number of bytes sent. The applications are responsible for checking that all data has been sent; if only some of them have been transmitted, the application should try to deliver the remaining data

  • socket.sendall(string[, flags]) : Send data to the socket. The socket must be connected to a remote socket. The optional argument flags has the same meaning as the previous one for recv (). A difference of send() , this method continues to send data from a string until all the data has been sent or until an error occurs. None is returned in case of success. In case of error, an exception is raised and there is no way to determine the amount of data, if any, that have been sent correctly .

Basically send() is a low level function, if you use it you should be in charge of verifying that all the data had actually been sent, on the contrary sendall() is a higher level implementation that assures you that they will be sent all data unless, of course, an error arises.

    
answered by 26.07.2018 / 05:58
source