sendto does not block. It's fire and forget. The stack does not care, if the packet you send hits the wire. If you deactivate your interface with the default gateway, then it still does not block. It raises an OSError without blocking.
Example:If you read (recv) from a socket, it blocks for UDP and TCP, except the socket is set to non-blocking mode. His provided example does not have one recv call. His code can not block, it should not block even if all interfaces are down and/or the internet connection is ultra-slow. It doesn't matter. It's not checked what a UDP packet does after the syscall.
Example:
Code:
import socketsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)sock.sendto(b"", ("1.2.3.4", 666))# no Exeption# turning the nic down# on my Desktop PC# sudo ip l set down eno1# On RPi:# sudo ip l set down eno1# sudo ip l set down wlan0sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)sock.sendto(b"", ("1.2.3.4", 666))# sock.sendto raises an Exception:# OSError: [Errno 101] Network is unreachable
Statistics: Posted by DeaD_EyE — Wed Aug 28, 2024 10:06 pm