I would like to know if socket.recvfrom in python is a blocking function ? I couldn't find my answer in the documentation If it isn't, what will be return if nothing is receive ? An empty string '' ? In the other case, if in fact, it is blocking, how can i do to put it as an unblocking function ? I heard about settimeout but I don't know if it is actually the right solution.
Unlike send(), the recv() function of Python's socket module can be used to receive data from both TCP and UDP sockets. The example client and server programs given here for UDP, use recv() at both client and the server sides.
General description. 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. Parameter Description socket.
The recvfrom() method can be used with an UDP server to receive data from a UDP client or it can be used with an UDP client to receive data from a UDP server.
UDP sockets use recvfrom to receive data. Its paremeter is the buffer size. The return value is a pair (data, address) where data is a byte string representing the data received and address is the address of the socket sending the data.
By default it is blocking. It can be turned into non-blocking via socket.setblocking(0)
or (equivalently) socket.settimeout(0)
. In that case if there is nothing to receive it will throw socket.error
exception. See the docs: https://docs.python.org/2/library/socket.html#socket.socket.setblocking
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With