To create a UDP server on a specific port in Python, you can use the built-in socket module. Below is an example code snippet that demonstrates how to create a UDP server on port 1234:
main.py360 chars15 lines
In this code snippet, we first create a UDP socket using socket.socket() function. We then specify the socket address family (socket.AF_INET) and the socket type (socket.SOCK_DGRAM), which indicates that we want to create a UDP socket.
Next, we bind the socket to a specific address and port using the bind() method. In this example, we bind the socket to the localhost interface on port 1234.
Finally, we enter a loop to listen for incoming messages using the recvfrom() method. When a message is received, we print the number of bytes received and the message content.
gistlibby LogSnag