Skip to content

Instantly share code, notes, and snippets.

@glegoux
Last active April 20, 2018 04:32
Show Gist options
  • Save glegoux/ddbddc5f2c741c9f5d899ea14941becc to your computer and use it in GitHub Desktop.
Save glegoux/ddbddc5f2c741c9f5d899ea14941becc to your computer and use it in GitHub Desktop.
[Pyhton] Run UDP server via socket server
#!/usr/bin/env python3
#
# udpserver.py
#
# See Gist udpclient.py to send a UDP message.
#
# Run UDP server on 127.0.0.1 port 8000.
#
# usage: python3 udpserver.py
if __name__ == "__main__":
import socket
UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT = 8000
# listen for UDP messages
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
server.bind((UDP_IP_ADDRESS, UDP_PORT))
print("Serving UDP on {} port {} ...".format(UDP_IP_ADDRESS, UDP_PORT))
while True:
data, addr = server.recvfrom(1024)
print("Received message: '{}'".format(data.decode('utf-8')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment