Discuss the difference and usage between send and sendall in python socket function

  • 2020-06-01 10:10:36
  • OfStack

In python socket programming, there are two functions that send TCP, send() and sendall(), as follows:

socket.send (string[, flags]) sends TCP data, returning the size of the bytes sent. This byte length may be less than the actual length of the data to be sent. In other words, if the function is executed once, it will not always be able to send the given data.

Example:


data = "something you want to send" 
while True: 
  len = s.send(data[len:]) 
  if not len: 
    break 

socket. sendall(string[, flags]) is easy to understand when you understand the above. Send the full TCP data, return None on success, fail to throw an exception

Example:


data = "something you want to send" 
 
s.sendall(data) 

Related articles: