Created
January 12, 2020 12:06
-
-
Save abdalmoez/9d09b2f69063b5f4919972e9971d2acd to your computer and use it in GitHub Desktop.
Start udp server and log received packets using python
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