-
-
Save snobu/1b6a6e67efd4d708cef61863e4b685a9 to your computer and use it in GitHub Desktop.
Simple python udp server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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