It doesn't really matter if your socket is in non-blocking mode or not, recv / send works almost the same way; the only difference is that a non-blocking socket generates a "Resource temporarily unavailable" error instead of waiting for data / socket.
Method
recv returns the number of bytes received, which are said to be less than or equal to the transmitted bufsize. If you want to get exactly the size of the bytes, you should do something similar to the following code:
def recvall(sock, size): data = '' while len(data) < size: d = sock.recv(size - len(data)) if not d:
It is important to remember that in lock mode you must do the same. (The number of bytes transferred to the application layer, for example, is limited by the size of the recv buffer in the OS.)
The send method returns the number of bytes sent, which is said to be less than or equal to the length of the transmitted string. If you want the whole message to be sent, you should do something similar to the following code:
def sendall(sock, data): while data: sent = sock.send(data) data = data[sent:]
You can directly use sock.sendall, but (according to the documentation) about the error, an exception is thrown and there is no way to determine how much data was sent successfully.
Sockets in Python follow the BSD API and behave similarly to c-style sockets (the difference, for example, is that they throw an exception instead of returning an error code). You should be happy with any online socket tutorial and manpages.
tomasz
source share