Improvement of UDP receiver.
This commit is contained in:
@@ -1,7 +1,19 @@
|
||||
from datetime import datetime
|
||||
import logging
|
||||
import queue
|
||||
import socket
|
||||
import struct
|
||||
import sys
|
||||
|
||||
|
||||
def setup_logging():
|
||||
FORMAT = "%(asctime)s %(message)s"
|
||||
logging.basicConfig(format=FORMAT, level=logging.INFO)
|
||||
logger = logging.getLogger()
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
handler.setLevel(logging.INFO)
|
||||
logger.addHandler(handler)
|
||||
return logger
|
||||
|
||||
|
||||
def get_ip_address():
|
||||
@@ -10,6 +22,8 @@ def get_ip_address():
|
||||
return s.getsockname()[0]
|
||||
|
||||
|
||||
logger = setup_logging()
|
||||
|
||||
multicast_group = "230.120.10.2"
|
||||
server_address = ("", 8123)
|
||||
|
||||
@@ -19,7 +33,7 @@ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
# Bind to the server address
|
||||
sock.bind(server_address)
|
||||
|
||||
print("Start")
|
||||
logging.info("Start")
|
||||
|
||||
# Tell the operating system to add the socket to the multicast group
|
||||
# on all interfaces.
|
||||
@@ -27,7 +41,7 @@ group = socket.inet_aton(multicast_group)
|
||||
mreq = struct.pack("4s4s", group, socket.inet_aton(get_ip_address()))
|
||||
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
|
||||
|
||||
print("Ready")
|
||||
logger.info("Ready")
|
||||
saveCounter = 0
|
||||
|
||||
buffer = queue.Queue(20)
|
||||
@@ -36,7 +50,7 @@ buffer = queue.Queue(20)
|
||||
while True:
|
||||
data, address = sock.recvfrom(1024)
|
||||
data_str = data.decode("utf-8").strip()
|
||||
print(address, ": ", data_str)
|
||||
logger.info(data_str)
|
||||
data_str = datetime.now().strftime("%b-%d-%Y_%H%M%S") + ": " + data_str
|
||||
buffer.put(data_str)
|
||||
if buffer.full():
|
||||
|
||||
Reference in New Issue
Block a user