Sending data via UDP
suggest changeUDP is a connectionless protocol. Messages to other processes or computers are sent without establishing any sort of connection. There is no automatic confirmation if your message has been received. UDP is usually used in latency sensitive applications or in applications sending network wide broadcasts.
The following code sends a message to a process listening on localhost port 6667 using UDP
UDP is connectionless, so sendto does not require connect. Close the socket when you are finished with it.
from socket import socket, AF_INET, SOCK_DGRAM
s = socket(AF_INET, SOCK_DGRAM)
msg = ("Hello you there!").encode('utf-8') # socket.sendto() takes bytes as input, hence we must encode the string first.
s.sendto(msg, ('localhost', 6667))
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents