#Python's UDP
User Datagram Protocol (UDP) is a connectionless, unreliable transport layer protocol, alongside TCP, and is one of the most common protocols on the Internet.
UDP programs do not have connections; they simply bind to their own address and send/receive data. The following example creates two sockets and sends data from one to the other.
import socket
# Create UDP sockets
sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind own addresses
sock1.bind(('0.0.0.0', 4000))
sock2.bind(('0.0.0.0', 4001))
# sock1 sends data to localhost:4001, which is sock2
sock1.sendto('hello\n'.encode(), ('localhost', 4001))
# sock2 receives data
data, addr = sock2.recvfrom(1024)
print(f'Received {data.decode()} from {addr}')
AF_INET
indicates using IPv4 addresses,SOCK_DGRAM
indicates using UDP- The address argument is a tuple; the first element can be a domain name, hostname, or IP address, and the second element is the port number.