Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python socket.sendall() function

Tags:

python

sockets

I'm reading Tutorial on Network Programming with Python, and in this document the author is saying that "The function sendall() should be used only with blocking sockets."

But I do not see any such condition in the Python documentation, socket.sendall(string[, flags]).

Is the author of PyNet right?

like image 280
nik Avatar asked Jun 05 '11 02:06

nik


People also ask

What does the socket accept () method return?

Upon successful completion, accept() shall return the non-negative file descriptor of the accepted socket. Otherwise, -1 shall be returned and errno set to indicate the error.

What does recv () return in Python?

The recv() function shall return the length of the message written to the buffer pointed to by the buffer argument. For message-based sockets, such as SOCK_DGRAM and SOCK_SEQPACKET, the entire message shall be read in a single operation.

What is the use of socket Recvfrom () method?

The recvfrom() function receives data on a socket named by descriptor socket and stores it in a buffer. The recvfrom() function applies to any datagram socket, whether connected or unconnected. The socket descriptor. The pointer to the buffer that receives the data.

What value is returned from the Python socket library expression S Connect_ex () If the socket is open?

You can use the function connect_ex. It doesn't throw an exception. Instead of that, returns a C style integer value (referred to as errno in C): s = socket.


2 Answers

When in doubt, check the source.

socket_sendall clearly gives up once send() returns -1, which it will do (with errno of EAGAIN or EWOULDBLOCK) if you call it on a non-blocking socket without calling poll() or select(). (And the internal_select function skips calling poll()/select() when the socket is non-blocking.)

So I would say the PyNet author is correct.

like image 98
Nemo Avatar answered Oct 23 '22 14:10

Nemo


sendall() doesn't make sense on non-blocking socket. It has to block if it can't send all data at once, otherwise it wouldn't be called "sendall".

like image 23
cababunga Avatar answered Oct 23 '22 15:10

cababunga