Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket python : recvfrom

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.

like image 396
AnonBird Avatar asked Jul 01 '15 16:07

AnonBird


People also ask

What is the use of socket Recvfrom () method in Python?

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.

What is socket Recvfrom?

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.

What is Recvfrom Python?

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.

What does Recvfrom return Python?

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.


1 Answers

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

like image 164
freakish Avatar answered Oct 13 '22 03:10

freakish