Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between socket.send() and socket.sendall()?

Tags:

python

sockets

I'm confused about socket.send() and socket.sendall() functions in Python. As I understand from the documentation send() function uses TCP protocol and sendall() function uses UDP protocol for sending data. I know that TCP is more reliable for most of the Web Applications because we can check which packets are sent and which packets are not. That's why, I think use of send() function can be more reliable rather than sendall() function.

At this point, I want to ask what is the exact difference between these two functions and which one is more reliable for web applications?

Thank you.

like image 541
cengineer Avatar asked Dec 13 '15 14:12

cengineer


People also ask

What is major difference between sent () and Sento () functions?

send(), sendto() These functions send data to a socket. Generally speaking, send() is used for TCP SOCK_STREAM connected sockets, and sendto() is used for UDP SOCK_DGRAM unconnected datagram sockets.

What is the use of socket Recvfrom () method?

recvfrom(data, address) − This method receives data from the socket. Two pair (data, address) value is returned by this method. Data defines the received data and address specifies the address of socket sending the data.

How do you send multiple messages in a socket Python?

Just run the code to start the server. The server is set to recv only 2 bytes at a time for demonstration purposes (it should be something like 8192). To send data to it import it (call it shut_srv or whatever) and call send_data for the client side.


1 Answers

socket.send is a low-level method and basically just the C/syscall method send(3) / send(2). It can send less bytes than you requested, but returns the number of bytes sent.

socket.sendall is a high-level Python-only method that sends the entire buffer you pass or throws an exception. It does that by calling socket.send until everything has been sent or an error occurs.

If you're using TCP with blocking sockets and don't want to be bothered by internals (this is the case for most simple network applications), use sendall.

And python docs:

Unlike send(), this method continues to send data from string until either all data has been sent or an error occurs. None is returned on success. On error, an exception is raised, and there is no way to determine how much data, if any, was successfully sent

Credits to Philipp Hagemeister for brief description I got in the past.

edit

sendall use under the hood send - take a look on cpython implementation. Here is sample function acting (more or less) like sendall :

def sendall(sock, data, flags=0):     ret = sock.send(data, flags)     if ret > 0:         return sendall(sock, data[ret:], flags)     else:         return None 

or from rpython (pypy source):

def sendall(self, data, flags=0, signal_checker=None):     """Send a data string to the socket.  For the optional flags     argument, see the Unix manual.  This calls send() repeatedly     until all data is sent.  If an error occurs, it's impossible     to tell how much data has been sent."""     with rffi.scoped_nonmovingbuffer(data) as dataptr:         remaining = len(data)         p = dataptr         while remaining > 0:             try:                 res = self.send_raw(p, remaining, flags)                 p = rffi.ptradd(p, res)                 remaining -= res             except CSocketError, e:                 if e.errno != _c.EINTR:                     raise             if signal_checker is not None:                 signal_checker() 
like image 64
kwarunek Avatar answered Sep 18 '22 09:09

kwarunek