create a udp server in python

Here's an example code to create a UDP server in Python:

main.py
import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024)
    print("Received message:", data)
226 chars
12 lines

In this code, we create a UDP socket and bind it to the IP address and port number specified by UDP_IP and UDP_PORT variables. The recvfrom method receives data from the socket and returns the data and the address of the sender. In this example, we simply print out the received message. The server runs in an infinite loop to continuously receive data.

gistlibby LogSnag