Skip to content

Instantly share code, notes, and snippets.

@duyet
Forked from majek/udp_server.py
Created January 3, 2020 04:56
Show Gist options
  • Save duyet/b26326013c7a54b57b0f749b5b239d18 to your computer and use it in GitHub Desktop.
Save duyet/b26326013c7a54b57b0f749b5b239d18 to your computer and use it in GitHub Desktop.
Simple python udp server
import logging
import socket
log = logging.getLogger('udp_server')
def udp_server(host='127.0.0.1', port=1234):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
log.info("Listening on udp %s:%s" % (host, port))
s.bind((host, port))
while True:
(data, addr) = s.recvfrom(128*1024)
yield data
FORMAT_CONS = '%(asctime)s %(name)-12s %(levelname)8s\t%(message)s'
logging.basicConfig(level=logging.DEBUG, format=FORMAT_CONS)
for data in udp_server():
log.debug("%r" % (data,))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment